[root@machine2 test]# for i in `seq 1 1 15`; do touch testing$i; done
[root@machine2 test]# ls
for.sh testing10 testing12 testing14 testing2 testing4 testing6 testing8
testing1 testing11 testing13 testing15 testing3 testing5 testing7 testing9
[root@machine2 test]# ls -lrth
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
[root@machine2 test]# ls -lrth > testdoc
To fetch only the column type $followed by the column number.
[root@machine2 test]# cat testdoc | awk '{print$1}'
total
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
[root@machine2 test]# cat testdoc | awk '{print$1,$2}'
total 4.0K
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
[root@machine2 test]# cat testdoc | awk '{print$1,$5}'
total
-rw-r--r-- 166
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
[root@machine2 test]# cat testdoc | awk '{print$1,$6}'
total
-rw-r--r-- Jan
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
[root@machine2 test]# cat testdoc | awk '{print$1,$9}'
total
-rw-r--r-- for.sh
-rw-r--r-- testing1
-rw-r--r-- testing2
-rw-r--r-- testing4
-rw-r--r-- testing3
-rw-r--r-- testing6
-rw-r--r-- testing5
-rw-r--r-- testing8
-rw-r--r-- testing7
-rw-r--r-- testing9
-rw-r--r-- testing10
-rw-r--r-- testing12
-rw-r--r-- testing11
-rw-r--r-- testing13
-rw-r--r-- testing14
-rw-r--r-- testing15
-rw-r--r-- testdoc
[root@machine2 test]# awk 'BEGIN {total=0} {total=total+$5} END {print total}' testdoc
166
The above syntax will print the sum of 5th column in the testdoc file. Here total is the variable assigned to zero in the begin syntax.
In the action block, the value of 5th column is passed for each row .After all values are processed, it prints the total value in END block.
[root@machine2 test]# awk -f totalscript testdoc
166
[root@machine2 test]# cat totalscript
#!/usr/bin/awk -f
BEGIN {total=0} {total=total+$5} END {print total}
We can use the script in a file and use -f option to run it.
[root@machine2 test]# awk '{if($9 == "testing9") print$0;}' testdoc
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing9
[root@machine2 test]# awk 'BEGIN{for(i=1;i<=10;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
square of 9 is 81
square of 10 is 100
[root@machine2 test]# awk -F: '{print$3}' /etc/passwd
0
1
2
3
4
5
6
7
8
10
11
12
13
14
99
81
170
69
32
499
173
68
38
498
89
27
70
29
65534
497
74
72
55
28
65
96
52
496
495
48
50022
75
107
141
14
Here -F is the field separator . We can also use awk 'BEGIN {FS=":"} {print $3}' /etc/passwd
[root@machine2 test]# awk '{print$8,$9}' testdoc
18:53 for.sh
17:43 testing1
17:43 testing2
17:43 testing4
17:43 testing3
17:43 testing6
17:43 testing5
17:43 testing8
17:43 testing7
17:43 testing9
17:43 testing10
17:43 testing12
17:43 testing11
17:43 testing13
17:43 testing14
17:43 testing15
17:46 testdoc
0
[root@machine2 test]# awk 'BEGIN{OFS=":"}{print$8,$9}' testdoc
:
18:53:for.sh
17:43:testing1
17:43:testing2
17:43:testing4
17:43:testing3
17:43:testing6
17:43:testing5
17:43:testing8
17:43:testing7
17:43:testing9
17:43:testing10
17:43:testing12
17:43:testing11
17:43:testing13
17:43:testing14
17:43:testing15
17:46:testdoc
OFS- Output Field separator , we add any character with this option
[root@machine2 test]# ls -l
total 12
-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
-rw-r--r-- 1 root root 847 Jun 9 17:46 testdoc
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing1
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing10
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing11
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing12
-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:43 testing2
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing3
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing4
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing5
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing6
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing7
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing8
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing9
-rw-r--r-- 1 root root 71 Jun 9 17:59 totalscript
[root@machine2 test]# awk '{print NF}' testdoc
2
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
NF - Number of field variables This will print number of columns in each row
[root@machine2 test]# awk '{print NR}' testdoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NR- Number of records, prints count of lines in a file or line number
For total line numbers type the below syntax
[root@machine2 test]# awk 'END{print NR}' testdoc
18
to search the contents in awk with out casesensitive IGNORECASE=1
[root@machine2 test]# ls
for.sh testing10 testing12 testing14 testing2 testing4 testing6 testing8
testing1 testing11 testing13 testing15 testing3 testing5 testing7 testing9
[root@machine2 test]# ls -lrth
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
[root@machine2 test]# ls -lrth > testdoc
To fetch only the column type $followed by the column number.
[root@machine2 test]# cat testdoc | awk '{print$1}'
total
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
[root@machine2 test]# cat testdoc | awk '{print$1,$2}'
total 4.0K
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
-rw-r--r-- 1
[root@machine2 test]# cat testdoc | awk '{print$1,$5}'
total
-rw-r--r-- 166
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
-rw-r--r-- 0
[root@machine2 test]# cat testdoc | awk '{print$1,$6}'
total
-rw-r--r-- Jan
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
-rw-r--r-- Jun
[root@machine2 test]# cat testdoc | awk '{print$1,$9}'
total
-rw-r--r-- for.sh
-rw-r--r-- testing1
-rw-r--r-- testing2
-rw-r--r-- testing4
-rw-r--r-- testing3
-rw-r--r-- testing6
-rw-r--r-- testing5
-rw-r--r-- testing8
-rw-r--r-- testing7
-rw-r--r-- testing9
-rw-r--r-- testing10
-rw-r--r-- testing12
-rw-r--r-- testing11
-rw-r--r-- testing13
-rw-r--r-- testing14
-rw-r--r-- testing15
-rw-r--r-- testdoc
[root@machine2 test]# awk 'BEGIN {total=0} {total=total+$5} END {print total}' testdoc
166
The above syntax will print the sum of 5th column in the testdoc file. Here total is the variable assigned to zero in the begin syntax.
In the action block, the value of 5th column is passed for each row .After all values are processed, it prints the total value in END block.
[root@machine2 test]# awk -f totalscript testdoc
166
[root@machine2 test]# cat totalscript
#!/usr/bin/awk -f
BEGIN {total=0} {total=total+$5} END {print total}
We can use the script in a file and use -f option to run it.
[root@machine2 test]# awk '{if($9 == "testing9") print$0;}' testdoc
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing9
[root@machine2 test]# awk 'BEGIN{for(i=1;i<=10;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
square of 9 is 81
square of 10 is 100
[root@machine2 test]# awk -F: '{print$3}' /etc/passwd
0
1
2
3
4
5
6
7
8
10
11
12
13
14
99
81
170
69
32
499
173
68
38
498
89
27
70
29
65534
497
74
72
55
28
65
96
52
496
495
48
50022
75
107
141
14
Here -F is the field separator . We can also use awk 'BEGIN {FS=":"} {print $3}' /etc/passwd
[root@machine2 test]# awk '{print$8,$9}' testdoc
18:53 for.sh
17:43 testing1
17:43 testing2
17:43 testing4
17:43 testing3
17:43 testing6
17:43 testing5
17:43 testing8
17:43 testing7
17:43 testing9
17:43 testing10
17:43 testing12
17:43 testing11
17:43 testing13
17:43 testing14
17:43 testing15
17:46 testdoc
0
[root@machine2 test]# awk 'BEGIN{OFS=":"}{print$8,$9}' testdoc
:
18:53:for.sh
17:43:testing1
17:43:testing2
17:43:testing4
17:43:testing3
17:43:testing6
17:43:testing5
17:43:testing8
17:43:testing7
17:43:testing9
17:43:testing10
17:43:testing12
17:43:testing11
17:43:testing13
17:43:testing14
17:43:testing15
17:46:testdoc
OFS- Output Field separator , we add any character with this option
[root@machine2 test]# ls -l
total 12
-rw-r--r-- 1 root root 166 Jan 25 18:53 for.sh
-rw-r--r-- 1 root root 847 Jun 9 17:46 testdoc
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing1
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing10
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing11
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing12
-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:43 testing2
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing3
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing4
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing5
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing6
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing7
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing8
-rw-r--r-- 1 root root 0 Jun 9 17:43 testing9
-rw-r--r-- 1 root root 71 Jun 9 17:59 totalscript
[root@machine2 test]# awk '{print NF}' testdoc
2
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
NF - Number of field variables This will print number of columns in each row
[root@machine2 test]# awk '{print NR}' testdoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NR- Number of records, prints count of lines in a file or line number
For total line numbers type the below syntax
[root@machine2 test]# awk 'END{print NR}' testdoc
18
to search the contents in awk with out casesensitive IGNORECASE=1
No comments:
Post a Comment