Linux centos7 awk tool

1. Introduction to awk

Awk is a powerful text analysis tool. Compared with grep's search and sed's editing, awk is particularly powerful when it analyzes data and generates reports. In short, awk covers all the functions of sed, reads the file line by line, slices each line with a space as the default delimiter, and then performs various analysis and processing on the cut part.

Second, on awk

 1. Intercept a fragment in the document

[root@davery ~]# mkdir awk                               creates an awk first
[root@davery ~]# cp /etc/passwd awk/0.txt
[root@davery ~]#
[root@davery ~]# cd awk

[root@davery awk]# head -n3 0.txt        显示前3行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@davery awk]#

[root@davery awk]# head -n3 0.txt |awk -F ':' '{print $0}' $0 is special for intercepting the whole line or

awk '{print $0}' 0.txt no need to specify separator

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@davery awk]#

 

[root@davery awk]# head -n3 0.txt |awk -F ':' '{print $1}'    displays the first 3 lines and the first field ($1) of
0.txt root
bin
daemon
[root@davery awk]#

[root@davery awk]# head -n3 0.txt |awk -F ':' '{print $2}'  displays the first 3 lines of 0.txt and the second field ($2)
x
x
x
[root@davery awk]# head -n3 0.txt |awk -F ':' '{print $3}'  displays the first 3 lines of 0.txt and the third field ($3)
0
1
2
[root@davery awk]# head -n2 0.txt |awk -F ':' '{print $6}' displays the first 2 lines of 0.txt and the sixth field ($6) /root /bin [root@davery awk]#  


[root@davery awk]# head -n3 0.txt |awk -F ':' '{print $1"#"$2"#"$3"#"}' can customize the format '{print $1"#"$2" #"$3"#"}'
root#x#0#
bin#x#1#
daemon#x#2#
[root@davery awk]#

[root@davery awk]# head -n3 0.txt |awk -F ':' '{print $1,$2,$3}'   put the first 3 lines of 0.txt, the 1st, 2nd, and 3rd fields ($1, $2 ,$3) show
root x 0
bin x 1
daemon x 2
[root@davery awk]#

2. Match characters or strings (find and display)

[root@davery awk]# awk '/ro/' 0.txt show all lines containing ro in the first paragraph
root:x:0:0:root:/root:/bin/bash
operator:x:11:0: operator:/root:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@davery awk]# awk -F ':' '$1 ~/oo/' 0 .txt ~ represents the meaning of matching, and displays the first field ($1) in 0.txt that contains oo
root:x:0:0:root:/root:/bin/bash
[root@davery awk]# awk -F ':' '$1 ~/ot/' 0.txt
root:x:0:0:root:/root:/bin/bash
[root@davery awk]# awk -F ':' '$1 ~/ root/' 0.txt
root:x:0:0:root:/root:/bin/bash
[root@davery awk]#

[root@davery awk]# awk -F ':' '/root/ {print $1,$3} /test/ {print $1.$3}' 0.txt Match root, then match test, support multiple expressions together
root 0
operator 11
[root@davery awk]#

3. Mathematical operations, == equals, >, >=, <, <=, !=

[root@davery awk]#awk -F ‘:’ '$3="0"' 0.txt

[root@davery awk]#awk -F ':' '$3>="500"' 0.txt plus "" for strings

 

[root@davery awk]#awk -F ':' '$3>=500' 0.txt   does not need "" for numbers

[root@davery awk]#awk -F ‘:’ '$7!="/sbin/nologin"' 0.txt

[root@davery awk]#awk -F ‘:’ '$3>$4' 0.txt  

[root@davery awk]#awk -F ‘:’ '$3>$4 && $3<"7" '0.txt 

[root@davery awk]#awk -F ‘:’ '$3>1000 || $7=="/bin/bash" 0.txt 

Second, under awk

awk built-in variables, commonly used variables include OFS, NF, NR, -F, which have similar functions and are used to define separators. When used as output, NF indicates how many segments are separated by separators, and NR indicates the line number.

[root@davery awk]#head -5 /etc/passwd |awk -F ':'  '{OFS="#"} {print $1,$3,$4}'

[root@davery awk]# head -5 /etc/passwd |awk -F ':' '{OFS="#"} {print $1,$3,$4}'
root#0#0
bin#1#1
daemon#2#2
adm#3#4
lp#4#7
[root@davery awk]#

[root@davery awk]#awk -F ':' '{OFS="#"} {if ($3>1000) {printf $1,$2,$3,$4}}' 0.txt

uaer1user2user3user4user6[root@davery awk]#

[root@davery awk]# head -n3 /etc/passwd |awk -F ':' '{print NF}'
7
7
7
[root@davery awk]#

[root@davery awk]# head -n3 /etc/passwd |awk -F ':' '{print NR}'
1
2
3
[root@davery awk]#

[root@davery awk]#awk 'NR>40' 0.txt

[root@davery awk]#awk -F ':' 'NR<20 && $1 ~/root/' 0.txt

root:x:0:0:root:/root:/bin/bash

[root@davery awk]#head -n 3 /etc/passwd |awk -F ':' '$1="root"' 

root x 0 0 root /root /bin/bash
root x 1 1 bin /bin /sbin/nologin
root x 2 2 daemon /sbin /sbin/nologin
[root@davery awk]#

[root@davery awk]#awk -F ':' '{(to=to+$3)}; END {print to}' 0.txt

[root@davery awk]# awk -F ':' '{(tot = tot+$ 3)}; END {print tot} '0.txt
8629
[root@davery awk]#

 

[root@davery awk]# awk -F ':' '{if($1=="root") {print $0}}' 0.txt
root:x:0:0:root:/root:/bin/bash
[root@davery awk]#

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325248797&siteId=291194637