Unix If else awk script - I have a file called \"iclassroster\" which has the fo
ID: 3867413 • Letter: U
Question
Unix If else awk script - I have a file called "iclassroster" which has the following data:
Last Name : First Name : Student ID : Program & Plan : Academic Level : ASURITE : Email
Alvarado : Anthony : 1209480997 : Ira A Fulton Engineering - Information Technology : Senior : aalvar33 : aalvar33@asu.edu
Alvarez : Gibran : 1201390350 : Ira A Fulton Engineering - Information Technology : Junior : galvare2 : galvare2@asu.edu
Angus : Glen : 1000746988 : Ira A Fulton Engineering - Information Technology : Junior : gangus : gangus@asu.edu
Ashby : Cole : 1207146326 : Ira A Fulton Engineering - Information Technology : Senior : cashby : cashby@asu.edu
Bailey : Allan : 1000548822 : Ira A Fulton Engineering - Information Technology : Senior : asu41981 : asu41981@asu.edu
Barron : Rudy : 1208428789 : Ira A Fulton Engineering - Information Technology : Junior : rrbarron : rrbarron@asu.edu
Box : Jeffrey : 1208427112 : Ira A Fulton Engineering - Information Technology : Post-Bacc Undergraduate : jabox1 : jabox1@asu.edu
Burden : Nicholas : 1206996878 : Ira A Fulton Engineering - Information Technology : Senior : nbburden : nbburden@asu.edu
Camiliere : Kurtis : 1200663364 : Ira A Fulton Engineering - Information Technology : Senior : kcamilie : kcamilie@asu.edu
Camp : Michael : 1000830925 : Ira A Fulton Engineering - Information Technology : Senior : mcamp1 : mcamp1@asu.edu
Carleton : Robert : 1000727027 : Ira A Fulton Engineering - Information Technology : Junior : rcarleto : rcarleto@asu.edu
Cartwright : Dustin : 1001460822 : Ira A Fulton Engineering - Tech Entrepreneurship & Mgmt : Senior : dbcartwr : dbcartwr@asu.edu
Cook : John : 1201860560 : Ira A Fulton Engineering - Information Technology : Senior : jrcook7 : jrcook7@asu.edu
Debiasio : Thomas : 1207331615 : Ira A Fulton Engineering - Software Engineering : Post-Bacc Undergraduate : tdebiasi : tdebiasi@asu.edu
Dixon : Jason : 1208487705 : Ira A Fulton Engineering - Software Engineering : Junior : jadixon5 : jadixon5@asu.edu
Fernandez : Jose : 1207627963 : Ira A Fulton Engineering - Information Technology : Post-Bacc Undergraduate : jlferna4 : jlferna4@asu.edu
Hale : Hunter : 1207103803 : Ira A Fulton Engineering - Information Technology : Junior : hehale : hehale@asu.edu
Heisey : Thane : 1207033252 : Ira A Fulton Engineering - Information Technology : Junior : theisey : theisey@asu.edu
Hoyle : Jeremy : 1000527662 : Ira A Fulton Engineering - Information Technology : Junior : jhoyle : jhoyle@asu.edu
Jardine : Parker : 1201654029 : Ira A Fulton Engineering - Information Technology : Post-Bacc Undergraduate : pjardine : pjardine@asu.edu
Johnson : Joshua : 1000745321 : Ira A Fulton Engineering - Information Technology : Junior : jrjohnso : jrjohnso@asu.edu
Kone : Chinmingnon : 1207997618 : Ira A Fulton Engineering - Information Technology : Junior : cdkone : cdkone@asu.edu
I need to create an If Else awk script to count the following: How many times does Information Technology occur? How many Freshmen there are? How many Sophmore there are? How many Juniors there are? How many Seniors there are? How many Post-Bacc Undergrads there are? and output this info to the screen.
So far I only have this and needs some help tooling my script to work for all the questions one after the other in an If elseif else structure. Please help I know it needs alot of work, but I have been tinkering with it for 2 days now. the letters were my count variables.
Explanation / Answer
There are easier approaches to do this without if else
The simplest script is
tr -s ' ' ' ' < classroster | grep Information | wc -l
tr -s ' ' ' ' < classroster | grep Freshman | wc -l
tr -s ' ' ' ' < classroster | grep Junior | wc -l
tr -s ' ' ' ' < classroster | grep Sophmore | wc -l
tr -s ' ' ' ' < classroster | grep Senior | wc -l
tr -s ' ' ' ' < classroster | grep Post | wc -l
If we need to use if else then
information=0;
freshman=0;
junior=0;
sophmore=0;
senior=0;
post=0;
for i in `cat $filename`;
do if [ $i == "information" ];
then ((information++))
elif [ $i == "freshman" ];
then ((freshman++))
elif [ $i == "junior" ];
then ((junior++))
elif [ $i == "sophmore" ];
then ((sophmore++))
elif [ $i == "senior" ];
then ((senior++))
elif [ $i == "post" ];
then ((post++))fi $i;
done;
echo $information;
echo $freshman;
echo $junior;
echo $sophmore;
echo $senior;
echo $post;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.