[root@centos-01 ~]# mkdir awk
[root@centos-01 ~]# cp /etc/passwd awk/test.txt
[root@centos-01 ~]# cd awk/
[root@centos-01 awk]# ls
test.txt

-F选项的作用是指定分隔符,如果不加-F指定,则以空格或者Tab为分隔符。print为打印的动作,用来打印出某个字段。$1为第一个字段,$2为第二个字段,依次类推,有一个特殊的那就是$0,它表示整行。

[root@centos-01 awk]# awk -F ':' '{print $1}' test.txt 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
postfix
sshd
chrony
[root@centos-01 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-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
[root@centos-01 awk]# awk -F ':' '{print $1,$7}' test.txt 
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
[root@centos-01 awk]# awk -F ':' '{print $1" # "$7}' test.txt 
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
[root@centos-01 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
[root@centos-01 awk]# awk -F ':' '$1 ~ /oo/' test.txt 
root:x:0:0:root:/root:/bin/bash
[root@centos-01 awk]# awk -F ':' '$1 ~ /po+/' test.txt 
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@centos-01 awk]# awk -F ':' '/root/ {print $1,$3} /mail/ {print $1,$3}' test.txt 
root 0
mail 8
operator 11
[root@centos-01 awk]# grep -E 'root|mail' test.txt 
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos-01 awk]# awk -F ':' '/root|mail/ {print $0}' test.txt 
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos-01 awk]# awk -F ':' '$3==0' test.txt 
root:x:0:0:root:/root:/bin/bash
[root@centos-01 awk]# awk -F ':' '$3==0 {print $1}' test.txt 
root
[root@centos-01 awk]# awk -F ':' '$3>=20 {print $1}' test.txt 
nobody
systemd-network
dbus
polkitd
postfix
sshd
chrony

在和数字比较时,若把比较的数字用双引号引起来后,那么awk不会认为是数字,而认为是字符,跟sort排序原理一样,按照ASCII码排序;不加双引号则认为是数字。

[root@centos-01 awk]# awk -F ':' '$3>="20" {print $1}' test.txt 
adm
lp
sync
shutdown
halt
mail
nobody
dbus
polkitd
postfix
sshd
chrony
[root@centos-01 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
[root@centos-01 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@centos-01 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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@centos-01 awk]# awk -F ':' '$3>5 && $3<7' test.txt 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@centos-01 awk]# awk -F ':' '$3>100 || $7=="/bin/bash"' test.txt 
root:x:0:0:root:/root:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@centos-01 awk]# awk -F ':' '$3>100 || $7 ~ /bash/' test.txt 
root:x:0:0:root:/root:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
  • awk内置变量OFS:字段分隔符
[root@centos-01 awk]# awk -F ':' '{OFS="#"} $3>100 {print $1,$3,$7}' test.txt 
systemd-network#192#/sbin/nologin
polkitd#999#/sbin/nologin
chrony#998#/sbin/nologin
[root@centos-01 awk]# awk -F ':' '{OFS="#"} {if ($3>100) {print $1,$3,$7}}' test.txt 
systemd-network#192#/sbin/nologin
polkitd#999#/sbin/nologin
chrony#998#/sbin/nologin
  • awk内置变量NR:已经读出的记录数,就是行号
[root@centos-01 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-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
  • awk内置变量NF:字段个数,就是列数
[root@centos-01 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-network:x:192:192:systemd Network Management:/:/sbin/nologin
7:dbus:x:81:81:System message bus:/:/sbin/nologin
7:polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
[root@centos-01 awk]# head -n 3 test.txt |awk -F ':' '$1=="root"'
root:x:0:0:root:/root:/bin/bash
[root@centos-01 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@centos-01 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@centos-01 awk]# awk -F ':' '{(tot=tot+$3)}; END {print tot}' test.txt 
2605