AWK Command

AWK Command with Examples in Unix/Linux

Unix/Linux AWK Command with Examples

In my earlier post I have shared my knowledge on SED Command with examples.

AWK stands for “Aho, Weinberger, and Kernighan”, these are the authors of AWK.

AWK Command used to makes text manipulation easier and simpler. It is mostly used for pattern search and processing.

Key features of AWK:

  • AWK Command reads a text file as records and fields.
  • AWK Commands has variables, conditionals and loops.
  • AWK Command has its own operators and can generate formatted output.

Syntax of AWK Command:

awk '/search_pattern1/ {Actions}
/search_pattern2/ {Actions}' filename

In above syntax,

  • Search_pattern is a regular expression.
  • Actions are statements to be performed.
  • Filename is input file.

How AWK Command Works:

  • It reads the input file, one line at one time.
  • For every line it matches the given pattern and if matches, perform the given action.
  • If no pattern matches then no action will be performed.
  • In the above syntax, either search pattern or actions are optional, But not both.
  • If the search pattern is not given, then AWK performs the given actions for each line of the input.
  • If the action is not given, print all that lines that matches with the given patterns which is the default action.
  • An empty brace without any action does nothing. It won’t perform default printing operation.
  • Each statement in Actions should be delimited by semicolon.

Examples of AWK Command:

First of all let’s create a file emp.txt, with following contents. And we will use these files throughout the example.

Linux AWK Command with Examples


AWK Command example 1: Default behavior of AWK

$ awk '{print;}' emp.txt

Linux AWK Command with Examples

So if you will use above command it will show the default behavior and print everything.


AWK Command Example 2. Print the lines which matches with the pattern

$ awk '/Sam/
> /Manmay/' emp.txt

Linux AWK Command with Examples

So as shown in figure, the above command will print all the lines that contains ‘Sam’ and ‘Manmay’


AWK Command Example 3. Print only specific field

Awk has number of built in variables. For each record i.e. line, it splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4. $0 represents whole line. NF is a built in variable which represents total number of fields in a record.

$ awk '{print $2,$5;}' emp.txt

Linux AWK Command with Examples

$ awk ‘{print $2,$NF;}’ emp.txt


AWK Command Example 4. BEGIN and END Keyword.

AWK has two Keyword BEGIN and END:

Syntax:

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

Above Actions in BEGIN Command will be executed before starts reading the lines from the input and END actions will be performed after completing the reading and processing the lines from the input.

$ awk 'BEGIN {print "Name\tDesignation\tDepartment\tSalary";}
> {print $2,"\t",$3,"\t",$4,"\t",$NF;}
> END {print "Report Generated\n--------------";
>}' emp.txt

Linux AWK Command with Examples

In the above example, the header part will be added with the help of BEGIN command.


AWK Command Example 5. Find the employees who has employee id greater than 300

$ awk '$1 >300' emp.txt

Linux AWK Command with Examples


 

One Reply to “AWK Command with Examples in Unix/Linux”

Leave a Reply