awk工具的使用
上面也提到了awk和sed一样是流式编辑器,它也是针对文档中的行来操作的,一行一行的去执行。awk比sed更加强大,它能做到sed能做到的,同样也能做到sed不能做到的。
分段打印出第三段
[[email protected] ~]# awk -F ‘:‘ ‘{print $3}‘ 1.txt 0 1 2 3 4 5 6 7 8 10 11 12 13 14 99 81 69 32 499 89 68 29 65534 74 500 多个段 [[email protected] ~]# awk -F ‘:‘ ‘OFS=":" {print $3,$4}‘ 1.txt 0:0 1:1 2:2 3:4 4:r_o7 5:0 6:0 7:0 8:12 10:14 : 11:0 12:100 13:30 14:50 99:99 81:81 69:69 32:32 499:76 89:89 68:68 29:29 65534:65a534 74:74 500:500 : : : 也可以用下面 [[email protected] ~]# awk -F ‘:‘ ‘OFS="#" {print $3,$4}‘ 1.txt 0#0 1#1 2#2 3#4 4#r_o7 5#0 6#0 7#0 8#12 10#14 # 11#0 12#100 13#30 14#50 99#99 81#81 69#69 32#32 499#76 89#89 68#68 29#29 65534#65a534 74#74 500#500 # # # 匹配 [[email protected] ~]# awk ‘/user/‘ 1.txt sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin 匹配多个 [[email protected] ~]# awk ‘/user|root/‘ 1.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin 贪婪匹配 只要第一个是r最后一个是o [[email protected] ~]# awk ‘/r.*o/‘ 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin ogames:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 一个或者零个 [[email protected] ~]# awk ‘/r.?o/‘ 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 一个或者多个 [[email protected] ~]# awk ‘/r.+o/‘ 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin ogames:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 匹配出一个或者多个 [[email protected] ~]# awk ‘/(oo)+/‘ 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologooin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nolooogin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown mail:x:8:12:mail:/var/spool/mail:/sbin/nolooogin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin ogames:x:12:100:games:/usr/games:/sbin/nologooin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin [[email protected] ~]# awk ‘/(ooo)+/‘ 1.txt lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nolooogin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown mail:x:8:12:mail:/var/spool/mail:/sbin/nolooogin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin [[email protected] ~]# grep -E --color ‘(o){2,10}‘ 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologooin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nolooogin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown mail:x:8:12:mail:/var/spool/mail:/sbin/nolooogin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin ogames:x:12:100:games:/usr/games:/sbin/nologooin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin 根据段匹配 [[email protected] ~]# awk -F ‘:‘ ‘$1~/r*o/‘ 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown operator:x:11:0:operator:/root:/sbin/nologin ogames:x:12:100:games:/usr/games:/sbin/nologooin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin 匹配出想要的段 [[email protected] ~]# awk -F ‘:‘ ‘$1~/r*o/ {print $3}‘ 1.txt 0 2 6 11 12 13 99 69 499 89 68 65534 多次匹配两个规则 [[email protected] ~]# awk -F ‘:‘ ‘$1~/r*o/ {print $1,$3}; $1~/user/ {print $1,$3}‘ 1.txt root 0 drooaemon 2 shutdown 6 operator 11 ogames 12 gopher 13 nobody 99 vcsoa 69 sasloauth 499 postfix 89 haldaemon 68 rpcuser 29 nfsnobody 65534 一个规则 [[email protected] ~]# awk -F ‘:‘ ‘$1~/r*o|nobody/ {print $1,$3}‘ 1.txt root 0 drooaemon 2 shutdown 6 operator 11 ogames 12 gopher 13 nobody 99 vcsoa 69 sasloauth 499 postfix 89 haldaemon 68 nfsnobody 65534
时间: 2024-12-22 11:03:00