Professional Documents
Culture Documents
------------------------------------------------------------------------
OPERATORS ==> (+ - * / % ^)
---------------------------------------------------------------------
AWK-IF-ELSE
Now let us create the sample input file which has the student marks.
$cat student-marks
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122 38 37
Edwin 2537 87 97 95
Dayan 2415 30 47
$ awk '{
if ($3 =="" || $4 == "" || $5 == "")
print "Some score for the student",$1,"is missing";'
}' student-marks
Some score for the student RinRao is missing
Some score for the student Dayan is missing
2. Awk If Else Example: Generate Pass/Fail Report based on Student marks in each subject
$ awk '{
if ($3 >=35 && $4 >= 35 && $5 >= 35)
print $0,"=>","Pass";
else
print $0,"=>","Fail";
}' student-marks
Jones 2143 78 84 77 => Pass
Gondrol 2321 56 58 45 => Pass
RinRao 2122 38 37 => Fail
Edwin 2537 87 97 95 => Pass
Dayan 2415 30 47 => Fail
3. Awk If Else If Example: Find the average and grade for every student
$ cat grade.awk
{
total=$3+$4+$5;
avg=total/3;
if ( avg >= 90 ) grade="A";
else if ( avg >= 80) grade ="B";
else if (avg >= 70) grade ="C";
else grade="D";
print $0,"=>",grade;
}
$ awk -f grade.awk student-marks
Jones 2143 78 84 77 => C
Gondrol 2321 56 58 45 => D
RinRao 2122 38 37 => D
Edwin 2537 87 97 95 => A
Dayan 2415 30 47 => D
-----------------------------------------------------------------------------------------
DO WHILE
$ awk 'BEGIN{
count=1;
do
print "This gets printed at least once";
while(count!=1)
}'
This gets printed at least once
----------------------------------------------------------------------------------------
FOR
3. Awk For Loop Example . Print the sum of fields in all lines.
$ awk '{ for (i = 1; i <= NF; i++) total = total+$i }; END { print total }'
12 23 34 45 56
34 56 23 45 23
351
4. Awk For Loop Example: Print the fields in reverse order on every line.
$ awk 'BEGIN{ORS="";}{ for (i=NF; i>0; i--) print $i," "; print "\n"; }' student-marks
77 84 78 2143 Jones
45 58 56 2321 Gondrol
37 38 2122 RinRao
95 97 87 2537 Edwin
47 30 2415 Dayan
--------------------------------------------------------------------------------------------------
BREAK
The above awk while loop prints the string “forever” forever, because the condition never get
fails. Now if you want to stop the loop after first 10 iteration, see the below script.
$ awk 'BEGIN{
x=1;
while(1)
{
print "Iteration";
if ( x==10 )
break;
x++;
}}'
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
----------------------------------------------------------------------------------------------
CONTINUE
Continue statement skips over the rest of the loop body causing the next cycle around the loop to
begin immediately.
6. Awk Continue Example: Execute the loop except 5th iteration
$ awk 'BEGIN{
x=1;
while(x<=10)
{
if(x==5){
x++;
continue;
}
print "Value of x",x;x++;
}
}'
Value of x 1
Value of x 2
Value of x 3
Value of x 4
Value of x 6
Value of x 7
Value of x 8
Value of x 9
Value of x 10
----------------------------------------------------------------------------------------------------------------------------
-
EXIT
7. Awk Exit Example: Exit from the loop at 5th iteration
$ awk 'BEGIN{
x=1;
while(x<=10)
{
if(x==5){
exit;}
print "Value of x",x;x++;
}
}'
Value of x 1
Value of x 2
Value of x 3
Value of x 4