Unix / Linux : 7 Awk Command Examples

0
4377

Awk is one of the most powerful tools in Unix used for processing the rows and columns in a file.Actually, you can process log files that contain maybe millions of lines to output a readable report that you can benefit from.With awk scripting language, you can make the following:

  • Define variables.
  • Use string and arithmetic operators.
  • Use control flow and loops.
  • Generate formatted reports.

Awk stands for the names of its authors “Aho, Weinberger, and Kernighan”

Consider a text file notes.txt to be processed with the following content −
~ emreozkan$ cat notes.txt 
1) Emre     Physics   10
2) Mehmet    Biology     85
3) Selim    Maths   81
4) Mustafa    History   78
5) Sema     English   68
Printing Column or Field

You can instruct AWK to print only certain columns from the input field.

Example:
~ emreozkan$ awk '{print $2 "\t" $3}' notes.txt

On executing this code, you get the following result −

Output:
Emre Physics
Mehmet Biology
Selim Maths
Mustafa History
Sema English

In the file notes.txt, the second column contains the student name and the third column contains the subject name.

Printing Columns by Pattern

Example:

~ emreozkan$ awk '/l/ {print $3 "\t" $4}' notes.txt
Output:
Selim Maths
Mustafa History
Sema English

Printing All Lines

Example:

~ emreozkan$ awk '/e/ {print $0}' notes.txt

On executing this code, you get the following results:

1) Emre     Physics   10
2) Mehmet    Biology     85
3) Selim    Maths   81
5) Sema     English   68

In the above example, we want to find “e” .The results that match what we want, it executes a command from the body block.

Printing Lines with More than 23 Characters

Example:

~ emreozkan$ awk 'length($0) > 23' notes.txt
1) Emre     Physics   10
2) Mehmet    Biology     85
4) Mustafa    History   78
5) Sema     English   68
Initialization and Final Action

Awk has two important patterns which are specified by the keyword called BEGIN and END.

Syntax: 

BEGIN { Actions}
{ACTION} # Action for everyline in a file
END { Actions }

# is for comments in Awk

Example:
awk 'BEGIN {print "Name\tSubjectName\tNotes";} 
{print $2,"\t",$3,"\t",$4,"\t",$NF;}
END{print "Report Generated\n--------------";
}' notes.txt


:~ emreozkan$ awk 'BEGIN {print "Name\tSubjectName\tNotes";} 
{print $2,"\t",$3,"\t",$4,"\t",$NF;}
END{print "Report Generated\n--------------";
}' notes.txt
Name SubjectName Notes
Emre   Physics   10   10
Mehmet   Biology   85   85
Selim   Maths   81   81
Mustafa   History   78   78
Sema   English   68   68
Report Generated
--------------

it prints headline and last file for the reports.

Print the list of Students in Physics

Class name is available third column.

Emre-MacBook-Air:~ emreozkan$ awk '$3 ~/Physics/' notes.txt 
1) Emre     Physics   10
Print number of Students in Maths Department
Emre-MacBook-Air:~ emreozkan$ awk 'BEGIN { count=0;}
> $3 ~ /Maths/ { count++; }
> END { print "Number of employees in Maths Dept =",count;}' notes.txt
Number of students in Maths Dept = 2

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.