test原文件內容:bash
[root@lgs-01 awk]# cat test 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-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin lgs:x:1008:1000::/home/lgs:/bin/bash abc:x:1001:1000::/home/abc:/bin/bash 12:22:55 587:1 12:21 2:2:1:fsfs:root:/bin/:/user 342:x:3d:fafa:toor:/user:/bin xx:xx:xx:xx:xx:xx:34 rr:rr:rr:rr:rr:rr:t55abc ##:#34#:bb:bb:bb:%pp^:$!# #234#:vv:vv:vv:vv:ds:@user#
答案:awk -F ':' '{print $0}' test
[root@lgs-01 awk]# awk -F ':' '{print $0}' test 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-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin lgs:x:1008:1000::/home/lgs:/bin/bash abc:x:1001:1000::/home/abc:/bin/bash 12:22:55 587:1 12:21 2:2:1:fsfs:root:/bin/:/user 342:x:3d:fafa:toor:/user:/bin xx:xx:xx:xx:xx:xx:34 rr:rr:rr:rr:rr:rr:t55abc ##:#34#:bb:bb:bb:%pp^:$!# #234#:vv:vv:vv:vv:ds:@user#
答案:awk -F ':' '$0 ~ /bash/' test
[root@lgs-01 awk]# awk -F ':' '$0 ~ /bash/' test root:x:0:0:root:/root:/bin/bash lgs:x:1008:1000::/home/lgs:/bin/bash abc:x:1001:1000::/home/abc:/bin/bash
答案:awk -F ':' '$3=="0" {print $0}' test
[root@lgs-01 awk]# awk -F ':' '$3=="0" {print $0}' test root:x:0:0:root:/root:/bin/bash
答案:awk -F ':' '$1=="root" {print $0}' test|sed -r 's/root/toor/g'
[root@lgs-01 awk]# awk -F ':' '$1=="root" {print $0}' test|sed -r 's/root/toor/g' toor:x:0:0:toor:/toor:/bin/bash
答案:awk -F ':' '{print $NF}' test
[root@lgs-01 awk]# awk -F ':' '{print $NF}' test /bin/bash /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /bin/sync /sbin/shutdown /sbin/halt /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 55 1 21 /user /bin 34 t55abc $!# @user#
答案:awk -F ':' 'NR>20 {print $0}' test
[root@lgs-01 awk]# awk -F ':' 'NR>20 {print $0}' test abc:x:1001:1000::/home/abc:/bin/bash 12:22:55 587:1 12:21 2:2:1:fsfs:root:/bin/:/user 342:x:3d:fafa:toor:/user:/bin xx:xx:xx:xx:xx:xx:34 rr:rr:rr:rr:rr:rr:t55abc ##:#34#:bb:bb:bb:%pp^:$!# #234#:vv:vv:vv:vv:ds:@user#
答案:awk -F ':' '$3<$4 {print $0}' test
[root@lgs-01 awk]# awk -F ':' '$3<$4 {print $0}' test 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 2:2:1:fsfs:root:/bin/:/user 342:x:3d:fafa:toor:/user:/bin
答案:awk -F ':' '{print$1"@"$NF}' test
[root@lgs-01 awk]# awk -F ':' '{print$1"@"$NF}' test root@/bin/bash bin@/sbin/nologin daemon@/sbin/nologin adm@/sbin/nologin lp@/sbin/nologin sync@/bin/sync shutdown@/sbin/shutdown halt@/sbin/halt mail@/sbin/nologin operator@/sbin/nologin games@/sbin/nologin ftp@/sbin/nologin nobody@/sbin/nologin systemd-network@/sbin/nologin dbus@/sbin/nologin polkitd@/sbin/nologin postfix@/sbin/nologin sshd@/sbin/nologin chrony@/sbin/nologin lgs@/bin/bash abc@/bin/bash 12@55 587@1 12@21 2@/user 342@/bin xx@34 rr@t55abc ##@$!# #234#@@user#
答案:awk -F ':' '{(sum=sum+$4)};END{print sum}' test
[root@lgs-01 awk]# awk -F ':' '{(sum=sum+$4)};END{print sum}' test 4704