Awk Command in Unix

Introduction Awk Command in Unix:

Contents

"<yoastmark

Awk Command in Unix is a programming language which allows easy manipulation of structured data and the generation of formatted reports. Awk stands for the names of its authors “Aho, Weinberger, and Kernighan”

Some of the key features of Awk are:

  • Awk views a text file as records and fields.
  • Like common programming language, Awk has variables, conditionals, and loops
  • Awk has arithmetic and string operators.
  • Awk can generate formatted reports.
awk 'pattern {action}' input-file

In the above awk syntax:

  • the search pattern is a regular expression(.
  • Actions – statement(s) to be performed.
  • several patterns and actions are possible in Awk.
  • file – Input file.
  • Single quotes around a program are to avoid shell not to interpret any of its special characters.

Awk Working Methodology:

  • Awk Command in Unix reads the input files one line at a time.
  • For each line, it matches with given pattern in the given order, if matches perform the corresponding action.
  • If no pattern matches, no action will be performed.
  • In the above syntax, either search pattern or action is 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 match with the given patterns which is the default action.
  • Empty braces without any action do nothing. It won’t performs default printing operation.
  • Each statement in Actions should be delimited by a semicolon.

examples mentioned below.

$cat employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

Awk script Examples:

The default behavior of Awk:

prints every line from the file

awk '{print;}' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

Awk print the lines which match with the pattern.

$ awk '/Thomas/
> /Nisha/' employee.txt
100  Thomas  Manager    Sales       $5,000
400  Nisha   Manager    Marketing   $9,500

Awk script Example 3. Print only specific field

$ awk '{print $2,$5;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000

Take Another example where we can see expression, conditions, and calculations:-

Example mention below:-

Justin Timberlake, Title 545, Price $7.302
 Taylor Swift, Title 723, Price $7.903,
Mick Jagger, Title 610, Price $7.904, 
Lady Gaga, Title 118, Price $7.305, 
Johnny Cash, Title 482, Price $6.506,
Elvis Presley, Title 335, Price $7.307,
John Lennon, Title 271, Price $7.908,
Michael Jackson, Title 373, Price $5.50

If the column separator is something other than spaces or tabs, such as a comma, you can specify that in the Awk Command in Unix as follows:

awk -F, '{ print $3 }' table1.txt > output1.txtTitle
 545Title
 723Title
 610Title
 118Title
 482…..

The list of statements inside the curly brackets (‘{‘,’}’) is called a block. If you put a conditional expression in front of a block, the statement inside the block will be executed only if the condition is true.

awk '$7=="\$7.30" { print $3 }' table1.txt

In this case, the condition is $7==”\$7.30″, which means that the element at column 7 is equal to $7.30. The backslash in front of the dollar sign is used to prevent the system from interpreting $7 as a variable and instead take the dollar sign literally.

So this Awk prints out the element in the 3rd column of each line that has a “$7.30” in column 7.

You can also use regular expressions as the condition. For example:

 awk '/30/ { print $3 }' table1.txt

The string between the two slashes (‘/’) is the regular expression. In this case, it is just the string “30.” This means if a line contains the string “30”, the system prints out the element at the 3rd column of that line. The output in the above example would be:

 Timberlake,
Gaga,
Presley,

If the table elements are numbers Awk can run calculations on them as in this example:

 awk'{ print ($2 * $3) + $7 }'

We can also define new variables as in this example:

 awk'{ sum=0; for (col=1; col<=NF; col++) sum += $col; print sum; }'

This computes and prints the sum of all the elements of each row.







Ashutosh Joshi

Devops Engineer having Experience in Gitlab CI-CD,Ansible,Rundeck,Sonarqube ,Docker ,Jfrog Artifact.
Automation testing experience in Katalone tool

Leave a Reply

Your email address will not be published. Required fields are marked *