Usage of awk Command to print and filter the data.

In this article I am going to show the basic usage of awk commands. I am using the below file to show the various usages.

[root@node1 ~]# cat data1
Item1,200
Item2,500
Item3,900
Item2,800
Item1,600

Case 1 : If you want to print the string or line equal to number of records in file. 

[root@node1 ~]# awk ‘{print “Hello world”}’ data1
Hello world
Hello world
Hello world
Hello world
Hello world

Case 2 : Lets add some spice by adding 1 at end of above expression. It means by default true condition. It will print string and each Record of file alternatively.

[root@node1 ~]# awk ‘{print “Hello world”}1’ data1
Hello world
Item1,200
Hello world
Item2,500
Hello world
Item3,900
Hello world
Item2,800
Hello world
Item1,600

Case 3 : Using the BEGIN in awk command to print the message before starting with processing on file. Now see the below two commands and compare the output with Case 1 and Case 2.

[root@node1 ~]# awk ‘BEGIN{print “Hello World”}’ data1
Hello World

[root@node1 ~]# awk ‘BEGIN{print “Hello World”}1’ data1
Hello World
Item1,200
Item2,500
Item3,900
Item2,800
Item1,600

Case 4 : How to print the selective columns in awk. By default Column separator or Field separator is space We have modified it to “,” in below case. Because our input is separated by “,” instead of blank space. where NF stands for number of fields.

[root@node1 ~]# awk -F, ‘{print $1,$2,NF}’ data1
Item1 200 2
Item2 500 2
Item3 900 2
Item2 800 2
Item1 600 2

Case 5 : As we are having BEGIN similarly we are having END statement as well which is used to print the data after processing of the file.

[root@node1 ~]# awk -F, ‘{print $1,$2;} END { print NR, ” records are processed”;}’ data1
Item1 200
Item2 500
Item3 900
Item2 800
Item1 600
5 records are processed

Case 6 : If we are changing NF to $NF then it is printing the value of last column.

[root@node1 ~]# awk -F, ‘{print $1,$2,$NF}’ data1
Item1 200 200
Item2 500 500
Item3 900 900
Item2 800 800
Item1 600 600

Case 7 : As I mentioned earlier by default filed separator is space we have changed it to “,”. In this case we are going to change it to new line. RS stands for record separator by default it is “\n” it has been changed also. Now it will show us 5 fields or columns are present which are separated by “\n”

[root@node1 ~]# awk ‘BEGIN { FS=”\n”; RS=””} {print $1,NF}’ data1
Item1,200 5

[root@node1 ~]# awk ‘BEGIN { FS=”\n”; RS=””} {print $1,NR}’ data1
Item1,200 1

[root@node1 ~]# awk ‘BEGIN { FS=”\n”; RS=””} {print $1,$2}’ data1
Item1,200 Item2,500

Case 8 : Similar to FS there is OFS (Output field separator) which by default is space and ORS (Output Record Separator) which by default is “\n”.

[root@node1 ~]# awk ‘BEGIN { FS=”\n”; RS=””; OFS=”,”} {print $1,$2}’ data1
Item1,200,Item2,500

[root@node1 ~]# awk ‘BEGIN {ORS=” “} {print $1} ‘ data1
Item1,200 Item2,500 Item3,900 Item2,800 Item1,600 [root@node1 ~]#

[root@node1 ~]# awk ‘BEGIN {ORS=”\n\n”} {print $1} ‘ data1
Item1,200

Item2,500

Item3,900

Item2,800

Item1,600

  •  Relational operator help to filter the output. I have put the output of vmstat command into file named data11.

[root@node1 ~]# cat data11
procs ———–memory———- —swap– —–io—- –system– —–cpu—–
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 91592 120576 250632 0 0 22 20 82 175 0 1 98 1 0
0 0 0 91568 120576 250636 0 0 0 0 70 154 0 0 100 0 0
0 0 0 91568 120576 250636 0 0 0 38 77 164 0 1 100 0 0
0 0 0 91568 120576 250636 0 0 0 30 78 185 0 1 93 6 0

Case 1 : Here I am applying filter on number of records greater than 2 and number of fields greater than 6. By filtering output in that way printing the first and fourth column.

[root@node1 ~]# awk ‘(NF>6 && NR > 2){print $1,$4}’ data11
0 91592
0 91568
0 91568
0 91568

Case 2 : Extending the above condition to CPU idle value.

[root@node1 ~]# awk ‘(NF>6 && NR > 2 && $15 < 95){print $1,$4}’ data11
0 91568

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s