9.6-9.7 awk 扩展 把这里面的所有练习题做一下 http://www.apelearn.com/study_v2/chapter14.html # 9.6 awk 上 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170827/145543851.png?imageslim) - 同样的 创建一个目录awk,把/etc/passwd 文件拷贝到 目录awk下,改名为test.txt ``` [[email protected] ~]# mkdir awk [[email protected] ~]# cp /etc/passwd awk/test.txt [[email protected] ~]# cd awk [[email protected] awk]# ls test.txt ``` - 匹配test.txt 文件第一列 用冒号分隔符分开 awk -F ‘:‘ ‘{print $1}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘{print $1}‘ test.txt root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-bus-proxy systemd-network dbus polkitd tss postfix sshd chrony aming user1 user2 user3 user4 user5 user6 saslauth [[email protected] awk]# ``` - 打印所有的段 awk -F ‘:‘ ‘{print $0}‘ test.txt 所有的段用 $0 表示, 第一段用$1 ``` [[email protected] awk]# awk -F ‘:‘ ‘{print $0}‘ test.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin aming:x:1000:1005::/home/aming:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - awk ‘{print $0}‘ test.txt 这个功能 有点像cat的 - awk 没有指定分隔符,默认会以空格,空白字符作为分隔符去打印 ``` [[email protected] awk]# awk ‘{print $0}‘ test.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin aming:x:1000:1005::/home/aming:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` --- - 来创建一个测试的文件 1.txt, awk ‘{print $1}‘ 1.txt 不指定分隔符,就以空格 空白字符作位分隔符 ``` [[email protected] awk]# vim 1.txt 1 2 aa bb dd ee [[email protected] awk]# awk ‘{print $1}‘ 1.txt 1 aa dd [[email protected] awk]# cat 1.txt 1 2 aa bb dd ee ``` -想要多看几段内容,中间用逗号隔开,awk -F ‘:‘ ‘{print $1,$3,$4}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘{print $1,$3,$4}‘ test.txt root 0 0 bin 1 1 daemon 2 2 adm 3 4 lp 4 7 sync 5 0 shutdown 6 0 halt 7 0 mail 8 12 operator 11 0 games 12 100 ftp 14 50 nobody 99 99 systemd-bus-proxy 999 997 systemd-network 192 192 dbus 81 81 polkitd 998 996 tss 59 59 postfix 89 89 sshd 74 74 chrony 997 995 aming 1000 1005 user1 1001 1001 user2 1002 1002 user3 1004 1005 user4 1006 1005 user5 1007 1007 user6 1008 1010 saslauth 996 76 [[email protected] awk]# ``` - 也可以指定中间以什么符号分割,比如用#号分割 awk -F ‘:‘ ‘{print $1"#"$3"#"$4}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘{print $1"#"$3"#"$4}‘ test.txt root#0#0 bin#1#1 daemon#2#2 adm#3#4 lp#4#7 sync#5#0 shutdown#6#0 halt#7#0 mail#8#12 operator#11#0 games#12#100 ftp#14#50 nobody#99#99 systemd-bus-proxy#999#997 systemd-network#192#192 dbus#81#81 polkitd#998#996 tss#59#59 postfix#89#89 sshd#74#74 chrony#997#995 aming#1000#1005 user1#1001#1001 user2#1002#1002 user3#1004#1005 user4#1006#1005 user5#1007#1007 user6#1008#1010 saslauth#996#76 [[email protected] awk]# ``` - [ ] awk匹配功能 - awk 把包含oo的行打印出来 ``` [[email protected] awk]# awk ‘/oo/‘ test.txt root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin [[email protected] awk]# ``` -匹配test.txt文件 第1段 包含oo的行 ``` [[email protected] awk]# awk -F ‘:‘ ‘$1 ~ /oo/‘ test.txt root:x:0:0:root:/root:/bin/bash [[email protected] awk]# ``` - awk 匹配也支持 正则表达式 awk -F ‘:‘ ‘$1 ~ /o+/‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘$1 ~ /o+/‘ test.txt root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown operator:x:11:0:operator:/root:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin [[email protected] awk]# awk -F ‘:‘ ‘$1 ~ /oo+/‘ test.txt root:x:0:0:root:/root:/bin/bash [[email protected] awk]# ``` - awk 支持多个表达式 awk -F ‘:‘ ‘/root/ {print $1,$3} /user/ {print $3,$4}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘/root/ {print $1,$3} /user/ {print $3,$4}‘ test.txt root 0 operator 11 59 59 1001 1001 1002 1002 1004 1005 1006 1005 1007 1007 1008 1010 996 76 [[email protected] awk]# grep -E ‘root|user‘ test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - 包含root 或者包含user的 全部打印出来 ``` [[email protected] awk]# awk -F ‘:‘ ‘/root|user/ {print $0}‘ test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - 针对数学 运算表达式的,这里的 第三段等于0 必须$3==0 俩个等于号,如果是一个 就成了shell变量了 ``` [[email protected] awk]# awk -F ‘:‘ ‘$3==0‘ test.txt root:x:0:0:root:/root:/bin/bash [[email protected] awk]# awk -F ‘:‘ ‘$3==0 {print $1}‘ test.txt root [[email protected] awk]# ``` -匹配第三段数字大于等于 1000的数字 打印第一行 ``` [[email protected] awk]# awk -F ‘:‘ ‘$3>=1000 {print $1}‘ test.txt aming user1 user2 user3 user4 user5 user6 ``` -匹配第三段大于等于1000的数字 并且打印全部行 ``` [[email protected] awk]# awk -F ‘:‘ ‘$3>=1000 {print $0}‘ test.txt aming:x:1000:1005::/home/aming:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash [[email protected] awk]# ``` - 注意数字加""双引号 和不加双引号的区别 加双引号会认为是一个字符串,以阿斯玛的排序方式计算的 如果是数字 不要加双引号 ``` [[email protected] awk]# awk -F ‘:‘ ‘$3>="1000" {print $0}‘ test.txt daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin aming:x:1000:1005::/home/aming:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - 第7段不等于 不等于用!=表示,如果是字符串,加上""双引号,"/sbin/login" ``` [[email protected] awk]# awk -F ‘:‘ ‘$7!="/sbin/nologin" {print $0}‘ test.txt root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt aming:x:1000:1005::/home/aming:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash [[email protected] awk]# ``` # 9.7 awk下 - 匹配 第三段数字小于第四段 awk -F ‘:‘ ‘$3<$4‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘$3<$4‘ test.txt adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin aming:x:1000:1005::/home/aming:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash [[email protected] awk]# ``` -匹配 第三段等于第四段数字的 ``` [[email protected] awk]# awk -F ‘:‘ ‘$3==$4‘ test.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user5:x:1007:1007::/home/user5:/bin/bash [[email protected] awk]# ``` - 匹配 第三段大于5小于7 awk -F ‘:‘ ‘$3>"5" && $3<"7"‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘$3>"5" && $3<"7"‘ test.txt shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin [[email protected] awk]# ``` - 匹配第三段大于数字1000 或者 第七段等于 /sbin/nologin - awk -F ‘:‘ ‘$3>1000 || $7=="/sbin/nologin"‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘$3>1000 || $7=="/sbin/nologin"‘ test.txt bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - 第三段大于1000 ,或者 第7段 带有/bash/ 的 - awk -F ‘:‘ ‘$3>1000 || $7 ~ /bash/‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘$3>1000 || $7 ~ /bash/‘ test.txt root:x:0:0:root:/root:/bin/bash aming:x:1000:1005::/home/aming:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash [[email protected] awk]# ``` - 变量OFS="#" 用来指定print 分隔符用#隔开,打印第1,第3,第7段 - awk -F ‘:‘ ‘{OFS="#"} $3>1000 || $7 ~ /bash/ {print $1,$3,$7}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘{OFS="#"} $3>1000 || $7 ~ /bash/ {print $1,$3,$7}‘ test.txt root#0#/bin/bash aming#1000#/bin/bash user1#1001#/bin/bash user2#1002#/bin/bash user3#1004#/bin/bash user4#1006#/sbin/nologin user5#1007#/bin/bash user6#1008#/bin/bash [[email protected] awk]# ``` - 不写条件,就是全部都用 # 号分隔符隔开 打印第1,3,7段 awk -F ‘:‘ ‘{OFS="#"} {print $1,$3,$7}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘{OFS="#"} {print $1,$3,$7}‘ test.txt root#0#/bin/bash bin#1#/sbin/nologin daemon#2#/sbin/nologin adm#3#/sbin/nologin lp#4#/sbin/nologin sync#5#/bin/sync shutdown#6#/sbin/shutdown halt#7#/sbin/halt mail#8#/sbin/nologin operator#11#/sbin/nologin games#12#/sbin/nologin ftp#14#/sbin/nologin nobody#99#/sbin/nologin systemd-bus-proxy#999#/sbin/nologin systemd-network#192#/sbin/nologin dbus#81#/sbin/nologin polkitd#998#/sbin/nologin tss#59#/sbin/nologin postfix#89#/sbin/nologin sshd#74#/sbin/nologin chrony#997#/sbin/nologin aming#1000#/bin/bash user1#1001#/bin/bash user2#1002#/bin/bash user3#1004#/bin/bash user4#1006#/sbin/nologin user5#1007#/bin/bash user6#1008#/bin/bash saslauth#996#/sbin/nologin [[email protected] awk]# ``` -匹配 第3段大于1000 ,并且用#作为后面打印1 2 3 4 段的分隔符 ``` [[email protected] awk]# awk -F ‘:‘ ‘{OFS="#"} {if ($3>1000 ) {print $1,$2,$3,$4}}‘ test.txt user1#x#1001#1001 user2#x#1002#1002 user3#x#1004#1005 user4#x#1006#1005 user5#x#1007#1007 user6#x#1008#1010 [[email protected] awk]# 或者 [[email protected] awk]# awk -F ‘:‘ ‘{OFS="#"} $3>1000 {print $1,$2,$3,$4}‘ test.txt user1#x#1001#1001 user2#x#1002#1002 user3#x#1004#1005 user4#x#1006#1005 user5#x#1007#1007 user6#x#1008#1010 [[email protected] awk]# ``` - 打印所有的行,把所有的行打印出来 awk -F ‘:‘ ‘{print NR":"$0}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘{print NR":"$0}‘ test.txt 1:root:x:0:0:root:/root:/bin/bash 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin 15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 16:dbus:x:81:81:System message bus:/:/sbin/nologin 17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin 18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin 19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin 22:aming:x:1000:1005::/home/aming:/bin/bash 23:user1:x:1001:1001::/home/user1:/bin/bash 24:user2:x:1002:1002::/home/user2:/bin/bash 25:user3:x:1004:1005::/home/user3:/bin/bash 26:user4:x:1006:1005::/home/aming111:/sbin/nologin 27:user5:x:1007:1007::/home/user5:/bin/bash 28:user6:x:1008:1010::/home/user6:/bin/bash 29:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - 把所有的段打印出来 awk -F ‘:‘ ‘{print NF":"$0}‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘{print NF":"$0}‘ test.txt 7:root:x:0:0:root:/root:/bin/bash 7:bin:x:1:1:bin:/bin:/sbin/nologin 7:daemon:x:2:2:daemon:/sbin:/sbin/nologin 7:adm:x:3:4:adm:/var/adm:/sbin/nologin 7:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 7:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 7:halt:x:7:0:halt:/sbin:/sbin/halt 7:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 7:operator:x:11:0:operator:/root:/sbin/nologin 7:games:x:12:100:games:/usr/games:/sbin/nologin 7:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 7:nobody:x:99:99:Nobody:/:/sbin/nologin 7:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin 7:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 7:dbus:x:81:81:System message bus:/:/sbin/nologin 7:polkitd:x:998:996:User for polkitd:/:/sbin/nologin 7:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin 7:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 7:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 7:chrony:x:997:995::/var/lib/chrony:/sbin/nologin 7:aming:x:1000:1005::/home/aming:/bin/bash 7:user1:x:1001:1001::/home/user1:/bin/bash 7:user2:x:1002:1002::/home/user2:/bin/bash 7:user3:x:1004:1005::/home/user3:/bin/bash 7:user4:x:1006:1005::/home/aming111:/sbin/nologin 7:user5:x:1007:1007::/home/user5:/bin/bash 7:user6:x:1008:1010::/home/user6:/bin/bash 7:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - 把test.txt第一行改成6段 ,再来打印 所有的段 ``` [[email protected] awk]# vim test.txt rootx:0:0:root:/root:/bin/bash [[email protected] awk]# awk -F ‘:‘ ‘{print NF":"$0}‘ test.txt 6:rootx:0:0:root:/root:/bin/bash 这里变成6段了 7:bin:x:1:1:bin:/bin:/sbin/nologin 7:daemon:x:2:2:daemon:/sbin:/sbin/nologin 7:adm:x:3:4:adm:/var/adm:/sbin/nologin 7:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 7:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 7:halt:x:7:0:halt:/sbin:/sbin/halt 7:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 7:operator:x:11:0:operator:/root:/sbin/nologin 7:games:x:12:100:games:/usr/games:/sbin/nologin 7:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 7:nobody:x:99:99:Nobody:/:/sbin/nologin 7:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin 7:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 7:dbus:x:81:81:System message bus:/:/sbin/nologin 7:polkitd:x:998:996:User for polkitd:/:/sbin/nologin 7:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin 7:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 7:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 7:chrony:x:997:995::/var/lib/chrony:/sbin/nologin 7:aming:x:1000:1005::/home/aming:/bin/bash 7:user1:x:1001:1001::/home/user1:/bin/bash 7:user2:x:1002:1002::/home/user2:/bin/bash 7:user3:x:1004:1005::/home/user3:/bin/bash 7:user4:x:1006:1005::/home/aming111:/sbin/nologin 7:user5:x:1007:1007::/home/user5:/bin/bash 7:user6:x:1008:1010::/home/user6:/bin/bash 7:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin [[email protected] awk]# ``` - NR作为一个判断条件 NR <= 小于等于10 也就是前10行 awk -F ‘:‘ ‘NR<=10‘ test.txt ``` [[email protected] awk]# awk -F ‘:‘ ‘NR<=10‘ test.txt rootx:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin [[email protected] awk]# ``` - 打印 前10行 并且第一段 带有root 或者 sync 的段 ``` [[email protected] awk]# awk -F ‘:‘ ‘NR<=10 && $1 ~ /root|sync/‘ test.txt rootx:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync [[email protected] awk]# ``` - 打印 前10行 并且 第一段 带有root 或者 sync 的段 第一段段只有6段的 只有第一行符合条件 ``` [[email protected] awk]# awk -F ‘:‘ ‘NF==6 && $1 ~ /root|sync/‘ test.txt rootx:0:0:root:/root:/bin/bash [[email protected] awk]# ``` - 有时候有这样的需求 ``` [[email protected] awk]# awk -F ‘:‘ ‘{print $NR":"$NF}‘ test.txt rootx:/bin/bash $1:$7 x:/sbin/nologin $2:$7 2:/sbin/nologin 4:/sbin/nologin lp:/sbin/nologin /sbin:/bin/sync /sbin/shutdown:/sbin/shutdown $7:$7 :/sbin/halt 因为只有7段,后面就是空的 :$7 :/sbin/nologin :$7 :/sbin/nologin :$7 :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/sbin/nologin :/bin/bash :/bin/bash :/bin/bash :/bin/bash :/sbin/nologin :/bin/bash :/bin/bash :/sbin/nologin :$7 [[email protected] awk]# ``` - 对test.txt前三行 赋值 第一段为root 一个等于号 就是赋值 ,俩个才是等于(匹配) ``` [[email protected] awk]# head -n 3 test.txt |awk -F ‘:‘ ‘$1="root"‘ root 0 0 root /root /bin/bash root x 1 1 bin /bin /sbin/nologin root x 2 2 daemon /sbin /sbin/nologin 用vim 把 文件内容第一段 改回来了 [[email protected] awk]# vim test.txt [[email protected] awk]# vim test.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin ``` -这个== 才是等于 匹配的意思 一个= 是赋值 ``` [[email protected] awk]# head -n 3 test.txt |awk -F ‘:‘ ‘$1=="root"‘ root:x:0:0:root:/root:/bin/bash [[email protected] awk]# ``` -发现赋值之后 分隔符没了,再定义分隔符 用冒号 :隔开 ``` [[email protected] awk]# head -n 3 test.txt |awk -F ‘:‘ ‘{OFS=":"} $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 [[email protected] awk]# ``` - 分段,然后是语句,这个tot 值 每次循环都会把第三段的值相加 - 第一次循环0加$3 ,第一行的第三段加 第二行的第三段 以此类推,最后求和一列 - 第三段所有的和 怎么求 ``` [[email protected] awk]# awk -F ‘:‘ ‘{(tot=tot+$3)}; END {print tot}‘ test.txt 11685 [[email protected] awk]# ``` # 扩展 - 把这里面的所有练习题做一下 - http://www.apelearn.com/study_v2/chapter14.html
时间: 2024-10-15 15:29:40