shallow丿ovehtml
[root@localhost ~]# mkdir awk [root@localhost ~]# cp /etc/passwd awk/test.txt [root@localhost ~]# cd awk/
[root@localhost awk]# awk -F ':' '{print $1}' test.txt root bin daemon adm lp sync shutdown halt mail operator games ftp nobody dbus polkitd avahi avahi-autoipd postfix sshd systemd-network zhangsan user1 tcpdump dockerroot
[root@localhost 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 dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin zhangsan:x:1000:1000::/home/zhangsan:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash tcpdump:x:72:72::/:/sbin/nologin dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost awk]# echo "1 2" >> 1.txt ;echo "a b" >> 1.txt ;echo "c d" >> 1.txt [root@localhost awk]# cat 1.txt 1 2 a b c d [root@localhost awk]# awk '{print $1}' 1.txt 1 a c
[root@localhost awk]# awk -F ':' '{print $1,$3,$5}' test.txt root 0 root bin 1 bin daemon 2 daemon adm 3 adm lp 4 lp sync 5 sync shutdown 6 shutdown halt 7 halt mail 8 mail operator 11 operator games 12 games ftp 14 FTP User nobody 99 Nobody dbus 81 System message bus polkitd 999 User for polkitd avahi 70 Avahi mDNS/DNS-SD Stack avahi-autoipd 170 Avahi IPv4LL Stack postfix 89 sshd 74 Privilege-separated SSH systemd-network 192 systemd Network Management zhangsan 1000 user1 1001 tcpdump 72 dockerroot 998 Docker User
[root@localhost awk]# awk -F ':' '{print $1","$3","$5}' test.txt root,0,root bin,1,bin daemon,2,daemon adm,3,adm lp,4,lp sync,5,sync shutdown,6,shutdown halt,7,halt mail,8,mail operator,11,operator games,12,games ftp,14,FTP User nobody,99,Nobody dbus,81,System message bus polkitd,999,User for polkitd avahi,70,Avahi mDNS/DNS-SD Stack avahi-autoipd,170,Avahi IPv4LL Stack postfix,89, sshd,74,Privilege-separated SSH systemd-network,192,systemd Network Management zhangsan,1000, user1,1001, tcpdump,72, dockerroot,998,Docker User
[root@localhost 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 dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost awk]# awk -F ':' '$1 ~ /oo/' test.txt root:x:0:0:root:/root:/bin/bash dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost 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 polkitd:x:999:998:User for polkitd:/:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt root 0 operator 11 user1 1001 1001 dockerroot 998
[root@localhost awk]# grep -E 'root|user' test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin user1:x:1001:1001::/home/user1:/bin/bash dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin [root@localhost awk]# awk '/root|user/ {print $0}' test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin user1:x:1001:1001::/home/user1:/bin/bash dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost awk]# awk -F ':' '$3==0' test.txt root:x:0:0:root:/root:/bin/bash [root@localhost awk]# awk -F ':' '$3==0 {print $1}' test.txt root [root@localhost awk]# awk -F ':' '$3>=1000 {print $1}' test.txt zhangsan user1 [root@localhost awk]# awk -F ':' '$3>=1000 {print $0}' test.txt zhangsan:x:1000:1000::/home/zhangsan:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash
[root@localhost 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 dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin zhangsan:x:1000:1000::/home/zhangsan:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash tcpdump:x:72:72::/:/sbin/nologin dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost 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 zhangsan:x:1000:1000::/home/zhangsan:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash
[root@localhost 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
[root@localhost awk]# awk -F ':' '$3=$4' test.txt bin x 1 1 bin /bin /sbin/nologin daemon x 2 2 daemon /sbin /sbin/nologin adm x 4 4 adm /var/adm /sbin/nologin lp x 7 7 lp /var/spool/lpd /sbin/nologin mail x 12 12 mail /var/spool/mail /sbin/nologin games x 100 100 games /usr/games /sbin/nologin ftp x 50 50 FTP User /var/ftp /sbin/nologin nobody x 99 99 Nobody / /sbin/nologin dbus x 81 81 System message bus / /sbin/nologin polkitd x 998 998 User for polkitd / /sbin/nologin avahi x 70 70 Avahi mDNS/DNS-SD Stack /var/run/avahi-daemon /sbin/nologin avahi-autoipd x 170 170 Avahi IPv4LL Stack /var/lib/avahi-autoipd /sbin/nologin postfix x 89 89 /var/spool/postfix /sbin/nologin sshd x 74 74 Privilege-separated SSH /var/empty/sshd /sbin/nologin systemd-network x 192 192 systemd Network Management / /sbin/nologin zhangsan x 1000 1000 /home/zhangsan /bin/bash user1 x 1001 1001 /home/user1 /bin/bash tcpdump x 72 72 / /sbin/nologin dockerroot x 995 995 Docker User /var/lib/docker /sbin/nologin
[root@localhost awk]# awk -F ':' '$3>"5" && $3<"7"' test.txt shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost awk]# awk -F ':' '$3>1000 && $3="/sbin/noligin"' test.txt user1 x /sbin/noligin 1001 /home/user1 /bin/bash
[root@localhost awk]# awk -F ':' '$3>1000 || $3="/sbin/noligin"' test.txt root x /sbin/noligin 0 root /root /bin/bash bin x /sbin/noligin 1 bin /bin /sbin/nologin daemon x /sbin/noligin 2 daemon /sbin /sbin/nologin adm x /sbin/noligin 4 adm /var/adm /sbin/nologin lp x /sbin/noligin 7 lp /var/spool/lpd /sbin/nologin sync x /sbin/noligin 0 sync /sbin /bin/sync shutdown x /sbin/noligin 0 shutdown /sbin /sbin/shutdown halt x /sbin/noligin 0 halt /sbin /sbin/halt mail x /sbin/noligin 12 mail /var/spool/mail /sbin/nologin operator x /sbin/noligin 0 operator /root /sbin/nologin games x /sbin/noligin 100 games /usr/games /sbin/nologin ftp x /sbin/noligin 50 FTP User /var/ftp /sbin/nologin nobody x /sbin/noligin 99 Nobody / /sbin/nologin dbus x /sbin/noligin 81 System message bus / /sbin/nologin polkitd x /sbin/noligin 998 User for polkitd / /sbin/nologin avahi x /sbin/noligin 70 Avahi mDNS/DNS-SD Stack /var/run/avahi-daemon /sbin/nologin avahi-autoipd x /sbin/noligin 170 Avahi IPv4LL Stack /var/lib/avahi-autoipd /sbin/nologin postfix x /sbin/noligin 89 /var/spool/postfix /sbin/nologin sshd x /sbin/noligin 74 Privilege-separated SSH /var/empty/sshd /sbin/nologin systemd-network x /sbin/noligin 192 systemd Network Management / /sbin/nologin zhangsan x /sbin/noligin 1000 /home/zhangsan /bin/bash user1:x:1001:1001::/home/user1:/bin/bash tcpdump x /sbin/noligin 72 / /sbin/nologin dockerroot x /sbin/noligin 995 Docker User /var/lib/docker /sbin/nologin
[root@localhost awk]# awk -F ':' '$3>1000 || $7 ~ /bash/' test.txt root:x:0:0:root:/root:/bin/bash zhangsan:x:1000:1000::/home/zhangsan:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash
[root@localhost 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 dbus,81,/sbin/nologin polkitd,999,/sbin/nologin avahi,70,/sbin/nologin avahi-autoipd,170,/sbin/nologin postfix,89,/sbin/nologin sshd,74,/sbin/nologin systemd-network,192,/sbin/nologin zhangsan,1000,/bin/bash user1,1001,/bin/bash tcpdump,72,/sbin/nologin dockerroot,998,/sbin/nologin
[root@localhost awk]# awk -F ':' '{OFS=","} $3>1000 || $7 ~ /bash/ {print $1,$3,$7}' test.txt root,0,/bin/bash zhangsan,1000,/bin/bash user1,1001,/bin/bash
[root@localhost awk]# awk -F ':' '{OFS=","} {if ($3>1000) {print $1,$3,$7}}' test.txt user1,1001,/bin/bash
[root@localhost 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:dbus:x:81:81:System message bus:/:/sbin/nologin 15:polkitd:x:999:998:User for polkitd:/:/sbin/nologin 16:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin 17:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin 18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 20:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 21:zhangsan:x:1000:1000::/home/zhangsan:/bin/bash 22:user1:x:1001:1001::/home/user1:/bin/bash 23:tcpdump:x:72:72::/:/sbin/nologin 24:dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost 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:dbus:x:81:81:System message bus:/:/sbin/nologin 7:polkitd:x:999:998:User for polkitd:/:/sbin/nologin 7:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin 7:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/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:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 7:zhangsan:x:1000:1000::/home/zhangsan:/bin/bash 7:user1:x:1001:1001::/home/user1:/bin/bash 7:tcpdump:x:72:72::/:/sbin/nologin 7:dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin
[root@localhost awk]# awk -F ':' 'NR<=10' 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
[root@localhost awk]# awk -F ':' 'NR<=10 && $1 ~ /root|sync/' test.txt root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync
[root@localhost awk]# awk -F ':' '{print $NR":"$NF}' test.txt root:/bin/bash x:/sbin/nologin 2:/sbin/nologin 4:/sbin/nologin lp:/sbin/nologin /sbin:/bin/sync /sbin/shutdown:/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 :/sbin/nologin :/bin/bash :/bin/bash :/sbin/nologin :/sbin/nologin
到第八行之後,$NR匹配爲空docker
[root@localhost awk]# head -n 3 test.txt | 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@localhost 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
[root@localhost awk]# head -n 3 test.txt | awk -F ':' '{OFS=":"} $1=="root"' root:x:0:0:root:/root:/bin/bash
[root@localhost awk]# awk -F ':' '{(tot=tot+$3)};END {print tot}' test.txt 4918
每一次循環都會把下一列的第三列值相加bash
awk的參考教程 http://www.cnblogs.com/emanlee/p/3327576.htmlssh