Untitled Document
awk Command

awk is very strong and most important command in Unix/ Linux Operating System . We have already covered the details about this command in chapter-14. Now lets see some more practical examples on awk command .

For our practice examples we have created one sample file emp.csv which has employeed details of an organisation .We will use this file as an input to our wak examples

1. Print the name of the employee whose designation is director -

   awk '/director/ { print }' emp.csv   

The above example can be written in following two ways -

   awk '/director/' emp.csv    
   awk '/director/ { print $0 }' emp.csv

2. Print Name, empid and salary of all the employess from a file -

   awk -F "," '{ print $1,$2,$3 }' emp.csv 

3. Print the same details as above , but only for employees from sr.no 3 to 6 -

   awk -F "," 'NR == 3 , NR == 6 { print $1,$2,$3 }' emp.csv 

4. Print the same details as above , but only for first five employees -

   awk -F "," 'NR == 1 , NR == 5 { print $1,$2,$3 }' emp.csv 

5. Print the list of employees whose Department is IT and Qaulity Assurance-

   awk -F "," '$4 == "IT" && 44 == "Quality Assurance" { print $1,$2,$3 }' emp.csv 

6. Print the list of employees whose Salary is greaer than 6000-

   awk -F "," '$3>6000 { print $1,$2,$3 }' emp.csv 

7. Print the list of employees whose Salary is less than 6000 and add 20% bonus to their salary and print the new salary-

awk -F "," '$3<6000 { print $1,$2,$3,($3*0.20)+$3 }' emp.csv

8. Write above command in a file and give that file as an input to awk command and see whether you get same results or not

   Here is the Input file emp.awk - write below code in a new file and save it as - emp.awk
   $3<6000 { print $1,$2,$3,($3*0.20)+$3 }

   We can now use awk with -f filename option to obtain the same output 
   awk -F"," -f emp.awk emp.csv  
If we use awk with -f option , make sure the program stored in the file is not enclosed within quotes.awk uses single quotes when the program is specified in the command line or when the entire awk command line is placed in a shell script .

9. For Pattern matching , we can use Regular Expression with awk -

In below Example we have use character class - which will match single character specified in brackets and displays the last name of employees matching the characters . So the Employee having last name Saxsena and Saksena can be matced in following way -

awk -F "," '/sa[kx]sena/' emp.csv

10. Regular Expression Operators applied on specific field with awk

We will consider above example , now as we know we have to match last name which is the second field of the csv file so we will use above awk command on second filed i.e $2 .

Note - Here we have to use the symbol ~ whenever we want to use awk on a filed . Just in case if you want to negate its meaning use !~ .

awk -F "," '$2 ~ /sa[kx]sena/' emp.csv
awk -F "," '$2 ~! /sa[kx]sena/' emp.csv

11. Formatting the awk output using printf command

Sometimes we want to format the awk output with proper header , space and allignment to get the well formatted output . Lets see the below example wchich add spacing between the fields and much better formatted output than usual awk command output .

awk -F "," '$6 > 5000 { printf "%-20s %-12s %d\n", $2,$3,$6}' emp.csv

12. Basic Example using BEGIN and END section

Lets Print the emp.csv file with header and footer as -

*** Begining Of File ***

*** End Of File ***

Run Below Command and You will see the header and footer will be added to your output .

awk 'BEGIN { print "*** Begining Of Line *** " } { print $0 } { print "*** End Of File ***" }' emp.csv

13. Basic Example using if statement -

Lets read the emp.csv file and check whether the designation or emp_id column is blank or not .
awk -F "," '{if ($1 =="" || $3 == "") print "either Designtaion or Emp_id is missing for" , $0;  }' emp.csv

14. Basic Example using if else statement -

Lets read the emp.csv file and print the list of employees who are getting salary greater than 6000 and not ..
awk -F "," '{ if ($6 > 6000) print $0,"Has salary greater than 6000" else print $0, "Has salary less than 6000"  }' emp.csv

15. Redirecting awk's output to a file -

Lets read the emp.csv file and print the list of employees who are getting salary greater than 6000 and redirect its ouput to another file emp1.csv
awk -F "," ' $6 > 6000 { print $0 }' emp.csv > emp1.csv
Untitled Document