AWK - Basic Examples

This tutorial describes several useful AWK commands along with appropriate examples. Consider we have a text file marks.txt to be processed and it has following content:
1)    Amit     Physics    80
2)    Rahul    Maths      90
3)    Shyam    Biology    87
4)    Kedar    English    85
                  5)    Hari     History    89

Print column or field

We can instruct AWK to print only certain columns from the input field. Below simple example illustrates this:
[jerry]$ awk '{print $3 "\t" $4}' marks.txt
On executing the above code, you get the following result:
Physics    80
Maths      90
Biology    87
English    85
History    89
In our marks.txt file 3rd column contains subject name and 4th column contains mark obtained in particular subject. And we are printing these two columns using AWK's print command. In above example $3 and $4 represents the third and fourth field from the input record.

Print all lines

By default AWK prints all the lines that match pattern.
[jerry]$ awk '/a/ {print $0}' marks.txt
On executing the above code, you get the following result:
2)    Rahul    Maths      90
3)    Shyam    Biology    87
4)    Kedar    English    85
5)    Hari     History    89
In above example we are searching form pattern a, when pattern match succeeds it executes command from the body block. In absence of body block - default action is taken which is print the record. Hence below command also produces the same result:
[jerry]$ awk '/a/' marks.txt

Print columns by pattern

We have seen when pattern match succeeds AWK prints entire record by default. But we can instruct AWK to print only certain fields. For instance below example prints 3rdand 4th field when pattern match succeeds.
[jerry]$ awk '/a/ {print $3 "\t" $4}' marks.txt
On executing the above code, you get the following result:
Maths      90
Biology    87
English    85
History    89

Print column in any order

Can we print columns in any order ? Yes ! Certainly we can do that. For instance below example prints 4th column followed by 3rd column.
[jerry]$ awk '/a/ {print $4 "\t" $3}' marks.txt
On executing the above code, you get the following result:
90    Maths
87    Biology
85    English
89    History

Count and print matched pattern

Let us try one more interesting example where we are going to count and print the number of lines for which pattern match succeeded.
[jerry]$ awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt
On executing the above code, you get the following result:
Count = 4
In above example, we increment value of counter when pattern match succeeds and we print this value in END block. Please note that unlike other programming languages there is no need to declare variable to use it.

Print lines having more than 18 characters

Let us print only those lines which contain more than 18 characters.
[jerry]$ awk 'length($0) > 18' marks.txt
On executing the above code, you get the following result:
3)    Shyam    Biology    87
4)    Kedar    English    85
AWK provides built-in length function which returns length of the string. $0 variable stores the entire line and in absence of body block default action is taken i.e. print action. Hence, if line has more than 18 characters then comparison results true and line gets printed.

No comments:

Post a Comment