You are on page 1of 6

awk '/foo/ {print}' file

search 'foo' in file and print the lines.

awk -F: '/[Ff]oo/{print}' file

*awk '$1 ~ /J/' inventory-shipped


-| Jan 13 25 15 115
-| Jun 31 42 75 492
-| Jul 24 34 67 436
-| Jan 21 36 64 620

*awk '$1 !~ /J/' inventory-shipped


-| Feb 15 32 24 226
-| Mar 15 24 34 228
-| Apr 31 52 63 420
-| May 16 34 29 208

awk '/[0-9]/' file

awk '/[a-z]/' file

awk '/abc|123/' file

awk '/^foo/{print}' file


search foo in the beggning of line.

awk '/foo$/{print}' file


search foo in the end of line.

awk '/foo/ {print $3}' file


search 3nd field for 'foo' and print.

awk '/foo/{print}' file | awk '{print $1,$2}'


print 1st and 2nd field of foo output.

awk '/foo/{print}' file | awk '{print $2,$1}'


print 2nd and 1st field of foo output.

awk '/foo/{print}' file | awk '{print "%-20s %12s\n" $1,$2}'


print 1st and 2nd field of foo output and creating space between the fields.
awk 'NR == 1, NR == 5 {print}' file
print line 1 to 5.

awk '$6 > 2000 {print}' file


print entries from 6th field greater then 2000.

------------------------------------------------------------------------

OPERATORS ==> (+ - * / % ^)

echo 10 20 | awk '{print $1+$2}'

awk '{print $1*$2}' acc

---------------------------------------------------------------------

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

1. Awk If Example: Check all the marks are exist

$ 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

2. Awk Do While Loop Example: Print the message at least once

$ 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

5. Awk Break Example: Awk Script to go through only 10 iteration

$ awk 'BEGIN{while(1) print "forever"}'

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

Awk Continue statement

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

You might also like