English 中文(简体)
AWK - Basic Examples
  • 时间:2024-09-08

AWK - Basic Examples


Previous Page Next Page  

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    Engpsh   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 "	" $4}  marks.txt

On executing this code, you get the following result −

Output

Physics   80
Maths     90
Biology   87
Engpsh   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 pnes 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    Engpsh   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 "	" $4}  marks.txt

On executing this code, you get the following result −

Output

Maths    90
Biology  87
Engpsh  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 "	" $3}  marks.txt

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

Output

90   Maths
87   Biology
85   Engpsh
89   History

Counting and Printing Matched Pattern

Let us see an example where you can count and print the number of pnes 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 unpke 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 pnes 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   Engpsh   85

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

Advertisements