条件操作符
[root@gz1 ~]# awk -F ':' '$1=="mysql"' 1.txt mysql:x:500:500::/home/mysql:/sbin/nologin
[root@gz1 ~]# awk -F ':' '$3>=500' 1.txt nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin
[root@gz1 ~]# awk -F ':' '$3<=500' 1.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin 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 sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown halt:x:7:0:halt:/sbin:/sbin/halt 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 gopher:x:13:30:gopher:/var/gopher:/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 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 haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 '''''''''''''''''''
[root@gz1 ~]# awk -F ':' '$7!="/sbin/nologin"' 1.txt root:x:0:0:root:/root:/bin/bash adm:riox:3:4:adm:/var/adm:/sbin/nologooin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nolooogin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nolooogin ogames:x:12:100:games:/usr/games:/sbin/nologooin 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 ''''''''''''''''''' 内置变量 awk OFS分隔符
[root@gz1 ~]# awk -F ':' 'OFS=":"; $3=$4' 1.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin drooaemon:x:2:2:daemon:/sbin:/sbin/nologin drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologooin adm:riox:4:4:adm:/var/adm:/sbin/nologooin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nolooogin lp:x:r_o7:r_o7:lp:/var/spool/lpd:/sbin/nolooogin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nolooogin mail:x:12:12:mail:/var/spool/mail:/sbin/nolooogin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin uucp:x:14:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin ogames:x:12:100:games:/usr/games:/sbin/nologooin ogames:x:100:100:games:/usr/games:/sbin/nologooin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin gopher:x:30:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin ftp:x:50:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsoa:x:69:69:virtual aconsole memory owner:/dev:/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 rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin sasloauth:x:76:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/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 nfsnobody:x:65a534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 '''''''''''''''''''
awk的NR行
把小于十的前九行打印出来
[root@gz1 ~]# awk -F ':' 'NR<10' 1.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin 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 sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdooown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nolooogin
把大于十行以后的行打印出来
[root@gz1 ~]# awk -F ':' 'NR>10' 1.txt 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 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 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 haldaemon:x:68:68:HAL daemon:/:/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 mysql:x:500:500::/home/mysql:/sbin/nologin 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 '''''''''''''''''''
把等于10打印出来
[root@gz1 ~]# awk -F ':' 'NR==10' 1.txt uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
只打印指定的段
[root@gz1 ~]# awk -F ':' 'NR==10{print $1,$7}' 1.txt uucp /sbin/nologin
awk NF段
查看有多少段
[root@gz1 ~]# awk -F ':' '{print NF}' 1.txt 7 7 7 7 7 7 7 7 7 7 0 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 1 1 1
awk的数学运算
[root@gz1 ~]# awk -F ':' 'OFS=":" {$7=$3+$4; print $0}' 1.txt root:x:0:0:root:/root:0 bin:x:1:1:bin:/bin:2 drooaemon:x:2:2:daemon:/sbin:4 adm:riox:3:4:adm:/var/adm:7 lp:x:4:r_o7:lp:/var/spool/lpd:4 sync:x:5:0:sync:/sbin:5 shutdown:x:6:0:shutdown:/sbin:6 halt:x:7:0:halt:/sbin:7 mail:x:8:12:mail:/var/spool/mail:20 uucp:x:10:14:uucp:/var/spool/uucp:24 ::::::0 operator:x:11:0:operator:/root:11 ogames:x:12:100:games:/usr/games:112 gopher:x:13:30:gopher:/var/gopher:43 ftp:x:14:50:FTP User:/var/ftp:64 nobody:x:99:99:Nobody:/:198 dbus:x:81:81:System message bus:/:162 vcsoa:x:69:69:virtual aconsole memory owner:/dev:138 rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:64 sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:575 postfix:x:89:89::/var/sp.oool/postfix:178 haldaemon:x:68:68:HAL daemon:/:136 rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:58 nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:65599 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:148 mysql:x:500:500::/home/mysql:1000 888888888888888888888888888888888888888888888888::::::0 &&&&&&&&&&&&&********************^^^^^^&^6::::::0 '''''''''''''''''''::::::0
只要其中的某些段
[root@gz1 ~]# awk -F ':' 'OFS=":" {$7=$3+$4; print $1,$3}' 1.txt root:0 bin:1 drooaemon:2 adm:3 lp:4 sync:5 shutdown:6 halt:7 mail:8 uucp:10 : operator:11 ogames:12 gopher:13 ftp:14 nobody:99 dbus:81 vcsoa:69 rpc:32 sasloauth:499 postfix:89 haldaemon:68 rpcuser:29 nfsnobody:65534 sshd:74 mysql:500 888888888888888888888888888888888888888888888888: &&&&&&&&&&&&&********************^^^^^^&^6: ''''''''''''''''''':
把所有的第三段相加求和
[root@gz1 ~]# awk -F ':' '{(sum=sum+$3)}; END {print sum}' 1.txt 67170
`