Linux正则awk的用法
作者:互联网
*awk相比grep/egrep/sed的功能更加强大,默认支持拓展正则表达式,grep需要加 -E 参数,sed需要加 -r 参数
1.分割文件内容打印指定段数:
[root@localhost ~]# cat test.txt
zhangsan 100
lisi 92
wangwu 95
user1 88
user2 93
[root@localhost ~]# awk '{print $1}' test.txt
zhangsan
lisi
wangwu
user1
user2
*默认以空格为分隔符,$指定打印的段数
2.-F参数:指定分隔符:
[root@localhost ~]# cat passwd.txt
AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '{print $3}' passwd.txt
nnnnny
linux01
#打印多段时用逗号区分
[root@localhost ~]# awk -F ':' '{print $1,$3}' passwd.txt
AAAA nnnnny
AAAA linux01
#指定打印结果中段与段的分割符号
[root@localhost ~]# awk -F ':' '{print $1"-->"$3}' passwd.txt
AAAA-->nnnnny
AAAA-->linux01
*awk {print $0}表示打印所有,相当于cat
3.awk检索:(相当于grep)
[root@localhost ~]# cat passwd.txt
AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk '/nnn/' passwd.txt
AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
4.检索指定段中包含关键字的行:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '$1 ~/AAA/' passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
5.多个表达式组合使用:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '/linux/ {print $1,$3} /nnn/ {print $2,$3}' passwd.txt
sed_test nnnnny
AAAA linux01
*打印包含linux关键字的行的第1和第3段,打印包含nnn关键字的第2和第3段
6.检索多个关键词的行并指定打印段:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '/linux|nnn/ {print $1}' passwd.txt
ABCD
AAAA
7.通过运算符号检索:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '$5==1000' passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '$5==1000 {print $1}' passwd.txt
AAAA
8.针对数字判断时,不加双引号,否则判断条件会被当做字符串而不是数字(数字当做字符串判断时,根据ASCII码判断,998大于1000):
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '$5<1000' passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
#加上双引号后1000被当做了字符串,而不是数字
[root@localhost ~]# awk -F ':' '$5<"1000"' passwd.txt
[root@localhost ~]#
9.字符串判断:(判断字符串时判断条件加上双引号)
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '$9!="/sbin/nologin"' passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
10.两个字段之间比较和多个条件检索:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '$5>$6' passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
[root@localhost ~]# awk -F ':' '$5>$6 {print $5,$6}' passwd.txt
998 996
[root@localhost ~]# awk -F ':' '$5>999' passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '$5>900 && $6<999' passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
[root@localhost ~]# awk -F ':' '$5>999 || $9=="/sbin/nologin"' passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
*最后一种字符串判断除了用==符号还可以用~检索
11.OFS:指定打印结果分隔符:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '{OFS="***"} {print $1,$2,$3}' passwd.txt
ABCD***sed_test***nnnnny
AAAA***sed_test***linux01
[root@localhost ~]# awk -F ':' '{OFS="***"} $5<1000 {print $1,$2,$3}' passwd.txt
ABCD***sed_test***nnnnny
12.NR:在每行前面显示行数:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '{print NR":" $0}' passwd.txt
1:ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
2:AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
13.NF:在每行前面显示段数:
[root@localhost ~]# awk -F ':' '{print NF":" $0}' passwd.txt
9:ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
9:AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
14.根据行数或者段数显示指定的行:
1)显示行号小于2的行:
[root@localhost ~]# awk -F ':' '{print NR":" $0}' passwd.txt
1:ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
2:AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk 'NR<2' passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
2)显示前10行并且第一段为AAAA的行:
[root@localhost ~]# awk -F ':' 'NR<10 && $1 ~/AAAA/' passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
15.=和==的区别:(==是匹配相等的数字或字符串,=是赋值)
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ":" '$1=="ABCD"' passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
[root@localhost ~]# awk -F ":" '$1="hello"' passwd.txt
hello sed_test nnnnny x 998 996 /var/lib/chronwy /sbin/nologin
hello sed_test linux01 x 1000 1000 /home/linux01 /bin/bash
16.求和:
[root@localhost ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[root@localhost ~]# awk -F ':' '{(tot=tot+$5)}; END {print tot}' passwd.txt
1998
标签:linux01,正则,sed,awk,Linux,test,root,localhost,1000 来源: https://blog.csdn.net/Powerful_Fy/article/details/101038253