AMP AMP

AWK command in Linux with Examples

AWK Command

AWK is a scripting language which is used to display contents of the file alone. The main advantage of this command is merging the data based on column to view the contents and it views only content of the file and doesn’ t make any alterations to the source file.

Syntax

awk ‘ < pattern> < action> ’ < input-file> < output-file>

We can take this following file as an example till the end.

[root@linuxhelp ~]# cat example-file 
Name,Marks,Max-Marks
User1,200,1000
User2,500,1000
User3,1000
User4,800,1000
User5,600,1000
User1,400,1000

To print the contents of file

This command just displays the contents of the file alone.

[root@linuxhelp ~]# awk ' {print }'  example-file 
Name,Marks,Max-Marks
User1,200,1000
User2,500,1000
User3,1000
User4,800,1000
User5,600,1000
User1,400,1000

To print only the specific fields

This command is used to print only the 2nd column and 3rd column.

[root@linuxhelp ~]# awk -F " ,"  ' {print $2, $3 }'  example-file 
Marks Max-Marks
200 1000
500 1000
1000
800 1000
600 1000
400 1000

-F option is used to specify the field separator.

To print the lines that matches the pattern

It is used to print only the line that matches the word “ User5, User1?

[root@linuxhelp ~]# awk ' /User1|User5/'  example-file 
User1,200,1000
User5,600,1000
User1,400,1000

To find unique values in a column

It displays the unique values in the 2nd column.

[root@linuxhelp ~]# awk -F, ' {a[$2] }END{for(i in a)print i }'  example-file 
200
400
500
Marks
600
800
1000

To find the total of all numbers in specific column

Using this command we can find out the total of all numbers in the specific column.

2nd Column
[root@linuxhelp ~]# awk -F" ,"    ' {x+=$2}  END{print x}'  example-file 
3500

3rd column
[root@linuxhelp ~]# awk -F" ,"    ' {x+=$3}  END{print x}'  example-file 
5000

To find the sum of individual group records with same name

It merges the sum of individual group records with same name and displays as one record.

[root@linuxhelp ~]# awk -F, ' {a[$1]+=$2 } END{for(i in a)print i" ," a[i] }'  example-file
User1,600
User2,500
User3,1000
User4,800
User5,600
Name,0

To find sum of all entries and append it to end

It displays the contents by adding in the specific column and appends the content in the end.

[root@linuxhelp ~]#awk -F” ,”  ‘ {x+=$2 y+=$3 print} END{print “ Total,” x,y}'  example-file 
Name,Marks,Max-Marks
User1,200,1000
User2,500,1000
User3,1000
User4,800,1000
User5,600,1000
User1,400,1000
Total,3500 5000    

Here, the total value is displayed at the last line of the column.

To print only the first record of every group

It is used to display the first occurrence of the group with the same name and leaves the next occurrence.

[root@linuxhelp ~]# awk -F, ‘ !a[$1]++’  example-file 
Name,Marks,Max-Marks
User1,200,1000
User2,500,1000
User3,1000
User4,800,1000
User5,600,1000

Here, the User1 is displayed only once in the first group.

To find the count of entries against every column based on the first column

This is used to find the count of entries against every column and it displays the count alone.

[root@linuxhelp ~]# awk -F, ‘ {a[$1]++ }END{for (i in a)print i, a[i] }’  example-file 
User1 2 
User2 1
User3 1
User4 1
User5 1
Name 1
Tag : awk
FAQ
Q
How to Print the lines which match with the given pattern using the awk command?
A
You can use the awk command with the following option to Print the lines which match with the given pattern. For Syntax: "awk '/manager/ {print}' employee.txt "
Q
How to print Lines containing a set of a word in a particular file in a separate line?
A
You can use the awk command with a certain option to print Lines containing a set of the word in a particular file in separate line. Syntax: "awk '/tom|jerry|vivek/' /etc/passwd"
Q
Give me the example to use the shell pipes with awk command?
A
Here I have to give the example to awk command with shell pipe. For EX: "history | awk '{print $2}' | sort | uniq -c | sort -rn | head".
Q
How to find count the line in a file using the awk command?
A
You can use the option of 'END {print NR}' with the awk command to find count the line in a certain file. Syntax: "awk 'END { print NR }' filename"
Q
How to set the field separator while using the awk command?
A
You can use the option of "-f" with awk command to set the filed separator as per your requirement.