AWK – Basic Examples

  • Post author:
  • Post category:AWK
  • Post comments:1 Comment
AWK - Basic Examples

This topic is about AWK – Basic Examples.

This chapter describes several useful AWK commands and their appropriate examples. Consider a text file marks.txt to be processed with the following content −

1) Amit     Physics   80
2) Rahul    Maths     90
3) Shyam    Biology   87
4) Kedar    English   85
5) Hari     History   89

Printing Column or Field

You can instruct AWK to print only certain columns from the input field. The following example demonstrates this −

Example

[jerry]$ awk '{print $3 "\t" $4}' marks.txt

On executing this code, you get the following result −

Output

Physics   80
Maths     90
Biology   87
English   85
History   89

In the file marks.txt, the third column contains the subject name and the fourth column contains the marks obtained in a particular subject. Let us print these two columns using AWK print command. In the above example, $3 and $4 represent the third and the fourth fields respectively from the input record.

Printing All Lines

By default, AWK prints all the lines that match pattern.

Example

[jerry]$ awk '/a/ {print $0}' marks.txt

On executing this code, you get the following result −

Output

2) Rahul    Maths     90
3) Shyam    Biology   87
4) Kedar    English   85
5) Hari     History   89

In the above example, we are searching form pattern a. When a pattern match succeeds, it executes a command from the body block. In the absence of a body block − default action is taken which is print the record. Hence, the following command produces the same result −

Example

[jerry]$ awk '/a/' marks.txt

Printing Columns by Pattern

When a pattern match succeeds, AWK prints the entire record by default. But you can instruct AWK to print only certain fields. For instance, the following example prints the third and fourth field when a pattern match succeeds.

Example

[jerry]$ awk '/a/ {print $3 "\t" $4}' marks.txt

On executing this code, you get the following result −

Output

Maths    90
Biology  87
English  85
History  89

Printing Column in Any Order

You can print columns in any order. For instance, the following example prints the fourth column followed by the third column.

Example

[jerry]$ awk '/a/ {print $4 "\t" $3}' marks.txt

On executing the above code, you get the following result −

Output

90   Maths
87   Biology
85   English
89   History

Counting and Printing Matched Pattern

Let us see an example where you can count and print the number of lines for which a pattern match succeeded.

Example

[jerry]$ awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt

On executing this code, you get the following result −

Output

Count = 4

In this example, we increment the value of counter when a pattern match succeeds and we print this value in the END block. Note that unlike other programming languages, there is no need to declare a variable before using it.

Printing Lines with More than 18 Characters

Let us print only those lines that contain more than 18 characters.

Example

[jerry]$ awk 'length($0) > 18' marks.txt

On executing this code, you get the following result −

Output

3) Shyam   Biology   87
4) Kedar   English   85

AWK provides a built-in length function that returns the length of the string. $0 variable stores the entire line and in the absence of a body block, default action is taken, i.e., the print action. Hence, if a line has more than 18 characters, then the comparison results true and the line gets printed.

I this topic we learned about AWK – Basic Examples. To know more, Click Here.

This Post Has One Comment

Leave a Reply