AWK Command

 AWK


  • The Awk is a powerful scripting language used for text scripting. It searches and replaces the texts and sorts, validates, and indexes the database.
  • It acts as a filter in Linux. It is also referred as gawk (GNU awk) In Linux.


Features of AWK command

  • It scans a file line by line.
  • It splits a file into multiple fields.
  • It compares the input text or a segment of a text file.
  • It performs various actions on a file like searching a specified text and more.
  • It formats the output lines.
  • It performs arithmetic and string operations.
  • It applies the conditions and loops on output.
  • It transforms the files and data on a specified structure.
  • It produces the format reports.


Syntax:

awk options 'selection _criteria {action }' input-file > output-file  


Built in variables:

-----------------------
FS  : field seperator
OFS  : Output Field seperator
RS  : Record seperator
ORS  : output record seperator
NR  : Number of records
NF  : Number of fields

It supports relational operators(<,<,<=,>=,==,!=)


[haritaraka12078@cxln4 ~]$ cat > student.txt
Sam CS
Daniel IT
John IT
Arya IT
Mike ECE
Helena ECE



-------------------------------------------------------------------------------------------------------------
Example1: List students with the specified pattern
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk '/ CS/ {print} ' student.txt
Sam CS



-------------------------------------------------------------------------------------------------------------
Example2: Default behaviour of awk command.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk '{print}' student.txt
Sam CS
Daniel IT
John IT
Arya IT
Mike ECE
Helena ECE



-------------------------------------------------------------------------------------------------------------
Example3: Print the specified column.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk '{print $1,$5}' student.txt
Sam
Daniel
John
Arya
Mike
Helena



-------------------------------------------------------------------------------------------------------------
Example4: Print the output and display the line number.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk '{print NR,$0}' student.txt
1 Sam CS
2 Daniel IT
3 John IT
4 Arya IT
5 Mike ECE
6 Helena ECE



-------------------------------------------------------------------------------------------------------------
Example5: Print the last field of the file
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk '{print $NF}' student.txt
CS
IT
IT
IT
ECE
ECE




-------------------------------------------------------------------------------------------------------------
Example6: Separate the output in the specified format.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk 'BEGIN { ORS ="-"} {print $0}' student.txt
Sam CS  -Daniel IT  -John IT  -Arya IT  -Mike ECE  -Helena ECE-[haritaraka12078@cxln4 ~]$




-------------------------------------------------------------------------------------------------------------
Example7: Print the square of the numbers from 1 to 8.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk 'BEGIN { for(i=1;i<=8;i++) print "square of", i, "is",i*i; }'
square of 1 is 1
square of 2 is 4
square of 3 is 9
square of 4 is 16
square of 5 is 25
square of 6 is 36
square of 7 is 49
square of 8 is 64



-------------------------------------------------------------------------------------------------------------
Example8: Calculate the sum of a particular column.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat >  StudentMarks
Name, Marks, Max marks
Sam,75,100
Daniel,80,100
John,74,100
Arya,85,100
Mike,70,100
Helena,74,100



[haritaraka12078@cxln4 ~]$ awk -F"," '{x+=$3}END{print x}' StudentMarks
600



-------------------------------------------------------------------------------------------------------------
Example9: Find some of the individual records.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk -F, '{a[$1]+=$2;}END{for(i in a)print i", "a[i];}' StudentMarks
Arya, 85
Daniel, 80
Mike, 70
John, 74
Sam, 75
Name, 0
Helena, 74




-------------------------------------------------------------------------------------------------------------
Example10: Find the value of exp 8.
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk 'BEGIN{x=exp(8); print x}'
2980.96




-------------------------------------------------------------------------------------------------------------
SOME MORE EXAMPLES
-------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ awk '{print $0}' StudentMarks
Name, Marks, Max marks
Sam,75,100
Daniel,80,100
John,74,100
Arya,85,100
Mike,70,100
Helena,74,100



[haritaraka12078@cxln4 ~]$ awk -F "," '{print $1}' StudentMarks
Name
Sam
Daniel
John
Arya
Mike
Helena



[haritaraka12078@cxln4 ~]$ awk -F "," '{print $3}' StudentMarks
 Max marks
100
100
100
100
100
100



[haritaraka12078@cxln4 ~]$ awk -F "," '{print $1,$3}' StudentMarks
Name  Max marks
Sam 100
Daniel 100
John 100
Arya 100
Mike 100
Helena 100




[haritaraka12078@cxln4 ~]$ awk -F "," '{print $1,"-",$3}' StudentMarks
Name -  Max marks
Sam - 100
Daniel - 100
John - 100
Arya - 100
Mike - 100
Helena - 100




[haritaraka12078@cxln4 ~]$ awk -F " "  'NR == 3; NR == 1 {print $0}' StudentMarks
Name, Marks, Max marks
Daniel,80,100




[haritaraka12078@cxln4 ~]$ awk -F " " '{print $1 ,"has salary", $3 }' StudentMarks
Name, has salary Max
Sam,75,100 has salary
Daniel,80,100 has salary
John,74,100 has salary
Arya,85,100 has salary
Mike,70,100 has salary
Helena,74,100 has salary




[haritaraka12078@cxln4 ~]$ awk -F " "  'NR>=3 {print $0}' StudentMarks
Daniel,80,100
John,74,100
Arya,85,100
Mike,70,100
Helena,74,100



[haritaraka12078@cxln4 ~]$ awk -F " "  'NR<=3 {print $0}' StudentMarks
Name, Marks, Max marks
Sam,75,100
Daniel,80,100



[haritaraka12078@cxln4 ~]$ awk -F " "  'NR<=3 {print $2}' StudentMarks
Marks,



[haritaraka12078@cxln4 ~]$ awk -F ","  'NR<=3 {print $2}' StudentMarks
 Marks
75
80



[haritaraka12078@cxln4 ~]$ awk -F ":"  'NR>1&&NR<3 {print $0}' StudentMarks
Sam,75,100




[haritaraka12078@cxln4 ~]$ awk -F ","  'NF>2 {print $2}' StudentMarks
 Marks
75
80
74
85
70
74



[haritaraka12078@cxln4 ~]$ awk -F " "  '{print NR}' StudentMarks
1
2
3
4
5
6
7



[haritaraka12078@cxln4 ~]$ awk -F ","  '{print NF}' StudentMarks
3
3
3
3
3
3
3



[haritaraka12078@cxln4 ~]$ awk -F ","  '$1=="Sam" {print $0}' StudentMarks
Sam,75,100




[haritaraka12078@cxln4 ~]$ awk -F ","  '$2==70 {print $0}' StudentMarks
Mike,70,100



[haritaraka12078@cxln4 ~]$ awk -F ","  '$1=="Sam" {print $1, $2, $3+11}' StudentMarks
Sam 75 111