Showing posts with label Advanced examples with awk command.. Show all posts
Showing posts with label Advanced examples with awk command.. Show all posts

Monday, August 5, 2013

Advanced examples with awk command.

[root@machine2 test]# awk '1;{print ""}' testdoc
total 4.0K                                      

-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing2

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing4

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing3

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing6

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing5

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing8

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing7

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing9

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing12

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing11

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing13

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing14

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing15

-rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc

[root@machine2 test]# awk 'BEGIN{ORS="\n\n"}' testdoc
ORS- output row separator

[root@machine2 test]# awk 'BEGIN{ORS="\n\n"};1' testdoc
total 4.0K                                          

-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing2

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing4

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing3

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing6

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing5

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing8

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing7

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing9

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing12

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing11

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing13

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing14

-rw-r--r-- 1 root root   0 Jun  9 17:43 testing15

-rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc


[root@machine2 test]# awk 'NF{print $0 "\n"}' testdoc

triple space a file

[root@machine2 test]# awk '1;{print "\n"}' testdoc
total 4.0K                                      


-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh


-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1


-rw-r--r-- 1 root root   0 Jun  9 17:43 testing2


-rw-r--r-- 1 root root   0 Jun  9 17:43 testing4


-rw-r--r-- 1 root root   0 Jun  9 17:43 testing3


-rw-r--r-- 1 root root   0 Jun  9 17:43 testing6

Print each line with its line number using thebelow syntax

[root@machine2 test]# awk '{print NR "\t" $0}' testdoc
1       total 4.0K                                  
2       -rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
3       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing1
4       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing2
5       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing4
6       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing3
7       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing6
8       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing5
9       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing8
10      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing7
11      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing9
12      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing10
13      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing12
14      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing11
15      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing13
16      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing14
17      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing15
18      -rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc
[root@machine2 test]# awk '{print FNR "\t" $0}' testdoc
1       total 4.0K
2       -rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
3       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing1
4       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing2
5       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing4
6       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing3
7       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing6
8       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing5
9       -rw-r--r-- 1 root root   0 Jun  9 17:43 testing8
10      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing7
11      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing9
12      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing10
13      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing12
14      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing11
15      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing13
16      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing14
17      -rw-r--r-- 1 root root   0 Jun  9 17:43 testing15
18      -rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc


[root@machine2 test]# awk '{printf("%5d : %s\n", NR,$0)}' testdoc
    1 : total 4.0K
    2 : -rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
    3 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing1
    4 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing2
    5 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing4
    6 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing3
    7 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing6
    8 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing5
    9 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing8
   10 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing7
   11 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing9
   12 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing10
   13 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing12
   14 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing11
   15 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing13
   16 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing14
   17 : -rw-r--r-- 1 root root   0 Jun  9 17:43 testing15
   18 : -rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc

Left align and right align using the above syntax

[root@machine2 test]# awk '{ total = total + NF }; END {print total}' testdoc
155
print the total number of fields ("words") in all lines

[root@machine2 test]# awk '{print $NF}' testdoc
4.0K
for.sh
testing1
testing2
testing4
testing3
testing6
testing5
testing8
testing7
testing9
testing10
testing12
testing11
testing13
testing14
testing15
testdoc

Prints the last filed in every line with above syntax
[root@machine2 test]# awk '{print NF ":" $0 }' testdoc
2:total 4.0K
9:-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing2
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing4
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing3
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing6
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing5
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing8
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing7
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing9
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing12
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing11
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing13
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing14
9:-rw-r--r-- 1 root root   0 Jun  9 17:43 testing15
9:-rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc

Prints number of fields followed by a character in each line.

[root@machine2 test]# awk '{test=$NF}; END{print test }' testdoc
testdoc

[root@machine2 test]# awk 'END{print $NF}' testdoc
testdoc

Find the last field of the last line using the above syntaxes

[root@machine2 test]# awk 'NF > 3' testdoc
-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing2
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing4
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing3
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing6
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing5
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing8
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing7
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing9
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing12
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing11
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing13
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing14
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing15
-rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc

Prints number of fields larger than 3

[root@machine2 test]# awk 'NF < 3' testdoc
total 4.0K

Prints number of fields less than 3

[root@machine2 test]# awk 'NR < 4' testdoc
total 4.0K
-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1

[root@machine2 test]# awk 'NR <=4' testdoc
total 4.0K
-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing2

Prints number of rows less than 4 and equal to 4 using above syntax.


[root@machine2 test]# awk '/total/' testdoc
total 4.0K
search for a pattern using above syntax.

[root@machine2 test]# awk '!/total/' testdoc
-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing1
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing2
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing4
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing3
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing6
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing5
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing8
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing7
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing9
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing12
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing11
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing13
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing14
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing15
-rw-r--r-- 1 root root   0 Jun  9 17:46 testdoc

Prints other than the searching pattern using above syntax.

[root@machine2 test]# awk '/total/; /for/' testdoc
total 4.0K
-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh

search multiple pattern using above syntax.

[root@machine2 test]# awk 'length> 48' testdoc
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing12
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing11
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing13
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing14
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing15

print only line above 48 characters through above syntax.

[root@machine2 test]# awk 'length< 15' testdoc
total 4.0K
prints less than 15 characters

[root@machine2 test]#  awk 'NR==8,NR==12' testdoc
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing5
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing8
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing7
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing9
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10

Prints specific rows from 8 to 12 with the above syntaxes

[root@machine2 test]#  awk 'NR==12' testdoc
-rw-r--r-- 1 root root   0 Jun  9 17:43 testing10

prints specific line.