六週第三次課

六週第三次課mysql

 9.6/9.7 awk 正則表達式

awk也是流式編輯器,針對文檔中的行來操做,一行一行地執行。sql

 

awk比sed更強大的功能是它支持了分段。apache

-F選項的做用是指定分隔符,若是不加-F選項,則以空格或者tab爲分隔符。print爲打印的動做,用來打印某個字段。$1爲第一個字段,$2爲第二個字段,依此類推。但$0比較特殊,它表示整行。vim

下面以:爲分隔符,打印test.txt文件的第一段。c#

 

[root@localhost awk]# awk -F ':' '{print $1}' test.txt 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
ods
pegasus
systemd-network
dbus
polkitd
apache
tomcat
colord
abrt
saslauth
libstoragemgmt
dirsrv
rpc
hsqldb
unbound
amandabackup
pkiuser
setroubleshoot
postfix
rtkit
pulse
qemu
ntp
radvd
chrony
named
mysql
tss
usbmuxd
geoclue
sssd
gdm
rpcuser
nfsnobody
pcp
kdcproxy
ipaapi
gnome-initial-setup
avahi
postgres
dovecot
dovenull
sshd
oprofile
tcpdump
tianqi
aming
user1
[root@localhost awk]# api

這種打印也不是刪除文本的某些段,至關於sed不加-i選項的做用,下面打印test.txtt的全部段。tomcat

[root@localhost 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
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:386:384::/var/lib/chrony:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
sssd:x:384:381:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]# bash

 

awk能夠直接打印所有文本。ssh

[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
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:386:384::/var/lib/chrony:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
sssd:x:384:381:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]# 

注意:awk的格式,-F後面緊跟單引號,單引號裏面爲分隔符。print動做要用{}括起來,不然會報錯。

print能夠打印自定義內容,可是自定義的內容要用「」括起來。

下面建立一下測試的文件。

[root@localhost awk]# vim 1.txt

1 2
aa bb
dd ee

保存,退出。

[root@localhost awk]# awk '{print $1}' 1.txt
1
aa
dd
[root@localhost awk]# cat 1.txt 
1 2
aa bb
dd ee
[root@localhost awk]# 

下面以:爲分隔符,打印test.txt文件的第1,3,4段。

[root@localhost 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
ods 999 998
pegasus 66 65
systemd-network 192 192
dbus 81 81
polkitd 998 996
apache 48 48
tomcat 91 91
colord 997 994
abrt 173 173
saslauth 996 76
libstoragemgmt 995 993
dirsrv 389 389
rpc 32 32
hsqldb 96 96
unbound 388 388
amandabackup 33 6
pkiuser 17 17
setroubleshoot 387 387
postfix 89 89
rtkit 172 172
pulse 171 171
qemu 107 107
ntp 38 38
radvd 75 75
chrony 386 384
named 25 25
mysql 27 27
tss 59 59
usbmuxd 113 113
geoclue 385 382
sssd 384 381
gdm 42 42
rpcuser 29 29
nfsnobody 65534 65534
pcp 383 380
kdcproxy 382 379
ipaapi 381 378
gnome-initial-setup 380 377
avahi 70 70
postgres 26 26
dovecot 97 97
dovenull 379 376
sshd 74 74
oprofile 16 16
tcpdump 72 72
tianqi 1000 1000
aming 1001 1001
user1 1002 1002
[root@localhost awk]#

[root@localhost awk]# awk -F ':' '{print $1"#"$3"#"$4}' test.tx
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
ods#999#998
pegasus#66#65
systemd-network#192#192
dbus#81#81
polkitd#998#996
apache#48#48
tomcat#91#91
colord#997#994
abrt#173#173
saslauth#996#76
libstoragemgmt#995#993
dirsrv#389#389
rpc#32#32
hsqldb#96#96
unbound#388#388
amandabackup#33#6
pkiuser#17#17
setroubleshoot#387#387
postfix#89#89
rtkit#172#172
pulse#171#171
qemu#107#107
ntp#38#38
radvd#75#75
chrony#386#384
named#25#25
mysql#27#27
tss#59#59
usbmuxd#113#113
geoclue#385#382
sssd#384#381
gdm#42#42
rpcuser#29#29
nfsnobody#65534#65534
pcp#383#380
kdcproxy#382#379
ipaapi#381#378
gnome-initial-setup#380#377
avahi#70#70
postgres#26#26
dovecot#97#97
dovenull#379#376
sshd#74#74
oprofile#16#16
tcpdump#72#72
tianqi#1000#1000
aming#1001#1001
user1#1002#1002
[root@localhost awk]# 

列出匹配oo的段。

[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
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@localhost awk]#

這跟sed的用法相似,能實現grep的功能,可是沒有顏色顯示,確定沒有grep用起來方便。

awk可讓某個段去匹配,~就是匹配的意思。

[root@localhost awk]# awk -F ':' '$1 ~ /oo/' test.txt 
root:x:0:0:root:/root:/bin/bash
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
[root@localhost awk]# 

 

awk支持正則表達式,並且不用加脫義符號。

[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
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:386:384::/var/lib/chrony:/sbin/nologin
geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
[root@localhost awk]# 

awk能夠進行屢次匹配,用:作分隔符,先匹配一項再匹配另一項。

[root@localhost awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $3,$4}' test.txt 
root 0
operator 11
996 76
389 389
33 6
17 17
107 107
75 75
59 59
113 113
29 29
379 376
16 16
1002 1002
[root@localhost awk]# 

 

[root@localhost awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt 
root 0
operator 11
saslauth 996 76
dirsrv 389 389
amandabackup 33 6
pkiuser 17 17
qemu 107 107
radvd 75 75
tss 59 59
usbmuxd 113 113
rpcuser 29 29
dovenull 379 376
oprofile 16 16
user1 1002 1002
[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
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]# 

匹配root或者user的行所有打印出來

[root@localhost 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
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]#

awk能夠用邏輯符號進行判斷,判斷符號有==(精確匹配)、>、>=、<、<=、!=。值得注意的是,在和數字比較時,若把比較的數字用雙引號引發來後,那麼awk不會認爲是數字,而認爲是字符,不加雙引號則認爲是數字。

打印第3段(uid)是0的行

 

[root@localhost awk]# awk -F ':' '$3==0' test.txt 
root:x:0:0:root:/root:/bin/bash

匹配第3段(uid)是0的行,並打印這些行的第1段。
[root@localhost awk]# awk -F ':' '$3==0 {print $1}' test.txt 
root

匹配第3段(uid)大於1000的行,並打印這些行的第1段。
[root@localhost awk]# awk -F ':' '$3>=1000 {print $1}' test.txt 
nfsnobody
tianqi
aming
user1
[root@localhost awk]# 

匹配第3段(uid)大於1000的行,並打印這些行.

[root@localhost awk]# awk -F ':' '$3>=1000 {print $0}' test.txt 
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]# 

若是這個1000加上雙引號那麼系統就不會把它當成數字,而是當成ACSII碼的方式去計算的,也就是說會認爲這個1000是字符串。

[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
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:386:384::/var/lib/chrony:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
sssd:x:384:381:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]#

列出第7段(不等於)不是/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
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]#

!=表示不匹配,它除了針對某一段的字符按進行比較外,還能夠在兩個段之間進行邏輯比較。

列出第3段小於第4段的行

 

[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]# 

列出第3段等於第4段的行

[root@localhost 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
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]#

另外還可使用 && 和 || 表示 「而且」 和 「或者」 的意思

列出第3段大於5(加雙引號的5算是字符)而且小於7的行。

[root@localhost awk]# awk -F ':' '$3>"5"&&$3<"7"' test.txt 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@localhost awk]# 

列出第3段大於1000或者第7段是/sbin/nologin的行。

[root@localhost 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
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:386:384::/var/lib/chrony:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
sssd:x:384:381:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]# 

列出第3段大於1000或者第7段包含bash的行。

[root@localhost awk]# awk -F ':' '$3>1000 || $7~/bash/' test.txt 
root:x:0:0:root:/root:/bin/bash
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]# 

awk的內置變量

awk經常使用的變量有:

OFS:和-F選項有相似的功能,也是用來定義分隔符的,可是它在輸出的時候定義。

NF:用分隔符分隔後一共有多少段。

NR:行數。

-F是分段前的分隔符,OFS是用來指定print時候要用到的分隔符。

列出第3段大於1000或者第7段包含bash的行,並打印出這些行的第1,3,7部分,而且以#將這些部分分割開。

[root@localhost awk]# awk -F ':' '{OFS="#"} $3>1000 || $7~/bash/ {print $1,$3,$7}' test.txt 
root#0#/bin/bash
amandabackup#33#/bin/bash
nfsnobody#65534#/sbin/nologin
postgres#26#/bin/bash
tianqi#1000#/bin/bash
aming#1001#/bin/bash
user1#1002#/bin/bash
[root@localhost awk]# 

先寫OFS,再寫條件,再寫print。

不寫條件的話,就是打印全部行的第1,3,7部分。

[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
ods#999#/sbin/nologin
pegasus#66#/sbin/nologin
systemd-network#192#/sbin/nologin
dbus#81#/sbin/nologin
polkitd#998#/sbin/nologin
apache#48#/sbin/nologin
tomcat#91#/sbin/nologin
colord#997#/sbin/nologin
abrt#173#/sbin/nologin
saslauth#996#/sbin/nologin
libstoragemgmt#995#/sbin/nologin
dirsrv#389#/sbin/nologin
rpc#32#/sbin/nologin
hsqldb#96#/sbin/nologin
unbound#388#/sbin/nologin
amandabackup#33#/bin/bash
pkiuser#17#/sbin/nologin
setroubleshoot#387#/sbin/nologin
postfix#89#/sbin/nologin
rtkit#172#/sbin/nologin
pulse#171#/sbin/nologin
qemu#107#/sbin/nologin
ntp#38#/sbin/nologin
radvd#75#/sbin/nologin
chrony#386#/sbin/nologin
named#25#/sbin/nologin
mysql#27#/sbin/nologin
tss#59#/sbin/nologin
usbmuxd#113#/sbin/nologin
geoclue#385#/sbin/nologin
sssd#384#/sbin/nologin
gdm#42#/sbin/nologin
rpcuser#29#/sbin/nologin
nfsnobody#65534#/sbin/nologin
pcp#383#/sbin/nologin
kdcproxy#382#/sbin/nologin
ipaapi#381#/sbin/nologin
gnome-initial-setup#380#/sbin/nologin
avahi#70#/sbin/nologin
postgres#26#/bin/bash
dovecot#97#/sbin/nologin
dovenull#379#/sbin/nologin
sshd#74#/sbin/nologin
oprofile#16#/sbin/nologin
tcpdump#72#/sbin/nologin
tianqi#1000#/bin/bash
aming#1001#/bin/bash
user1#1002#/bin/bash
[root@localhost awk]#

咱們也能夠加語句進行修飾,這裏就使用到了if。

[root@localhost awk]# awk -F ':' '{OFS="#"} {if ($3>1000) {print $1,$2,$3,$4}}' test.txt 
nfsnobody#x#65534#65534
aming#x#1001#1001
user1#x#1002#1002
[root@localhost awk]# 

也能夠寫成下面的形式,只是有點生硬。

[root@localhost awk]# awk -F ':' '{OFS="#"} $3>1000 {print $1,$2,$3,$4}' test.txt 
nfsnobody#x#65534#65534
aming#x#1001#1001
user1#x#1002#1002
[root@localhost awk]# 

在列出的結果中前面顯示行號加冒號(NR的意思是加行號)。

[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:ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
15:pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
16:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
17:dbus:x:81:81:System message bus:/:/sbin/nologin
18:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
19:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
20:tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
21:colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
22:abrt:x:173:173::/etc/abrt:/sbin/nologin
23:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
24:libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
25:dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
26:rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
27:hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
28:unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
29:amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
30:pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
31:setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
32:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
33:rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
34:pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
35:qemu:x:107:107:qemu user:/:/sbin/nologin
36:ntp:x:38:38::/etc/ntp:/sbin/nologin
37:radvd:x:75:75:radvd user:/:/sbin/nologin
38:chrony:x:386:384::/var/lib/chrony:/sbin/nologin
39:named:x:25:25:Named:/var/named:/sbin/nologin
40:mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
41:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
42:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
43:geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
44:sssd:x:384:381:User for sssd:/:/sbin/nologin
45:gdm:x:42:42::/var/lib/gdm:/sbin/nologin
46:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
47:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
48:pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
49:kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
50:ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
51:gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
52:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
53:postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
54:dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
55:dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
56:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
57:oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
58:tcpdump:x:72:72::/:/sbin/nologin
59:tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
60:aming:x:1001:1001::/home/aming:/bin/bash
61:user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]#

在列出的結果中前面顯示該行有多少段,加冒號(NF的意思是加段數)。

[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:ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
7:pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
7:tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
7:colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
7:abrt:x:173:173::/etc/abrt:/sbin/nologin
7:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
7:libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
7:dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
7:rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
7:hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
7:unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
7:amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
7:pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
7:setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
7:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
7:rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
7:pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
7:qemu:x:107:107:qemu user:/:/sbin/nologin
7:ntp:x:38:38::/etc/ntp:/sbin/nologin
7:radvd:x:75:75:radvd user:/:/sbin/nologin
7:chrony:x:386:384::/var/lib/chrony:/sbin/nologin
7:named:x:25:25:Named:/var/named:/sbin/nologin
7:mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
7:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
7:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
7:geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
7:sssd:x:384:381:User for sssd:/:/sbin/nologin
7:gdm:x:42:42::/var/lib/gdm:/sbin/nologin
7:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
7:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
7:pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
7:kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
7:ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
7:gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
7:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
7:postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
7:dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
7:dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
7:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
7:oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
7:tcpdump:x:72:72::/:/sbin/nologin
7:tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
7:aming:x:1001:1001::/home/aming:/bin/bash
7:user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]#

下面改一下test.txt,將第1行改爲6段的。

[root@localhost awk]# vim test.txt 
[root@localhost awk]# awk -F ':' '{print NF":" $0}' test.txt 
6:rootx: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:ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
7:pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
7:tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
7:colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
7:abrt:x:173:173::/etc/abrt:/sbin/nologin
7:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
7:libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
7:dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
7:rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
7:hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
7:unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
7:amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
7:pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
7:setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
7:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
7:rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
7:pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
7:qemu:x:107:107:qemu user:/:/sbin/nologin
7:ntp:x:38:38::/etc/ntp:/sbin/nologin
7:radvd:x:75:75:radvd user:/:/sbin/nologin
7:chrony:x:386:384::/var/lib/chrony:/sbin/nologin
7:named:x:25:25:Named:/var/named:/sbin/nologin
7:mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
7:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
7:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
7:geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
7:sssd:x:384:381:User for sssd:/:/sbin/nologin
7:gdm:x:42:42::/var/lib/gdm:/sbin/nologin
7:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
7:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
7:pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
7:kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
7:ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
7:gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
7:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
7:postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
7:dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
7:dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
7:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
7:oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
7:tcpdump:x:72:72::/:/sbin/nologin
7:tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
7:aming:x:1001:1001::/home/aming:/bin/bash
7:user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost awk]#

把行數小於等於10的列出來,也就是打印前十行。

[root@localhost 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
[root@localhost awk]# 

打印行數小於等於10,且第1段包含root或者sync的行。

[root@localhost 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
[root@localhost awk]# 

打印段數等於6的行,且第1段包含root或者sync。

[root@localhost awk]# awk -F ':' 'NF==6 && $1 ~ /root|sync/' test.txt 
rootx:0:0:root:/root:/bin/bash
[root@localhost awk]#

只有以前改動的第1行符合要求。

顯示(該行的行號所對應的段數內容):如第一行就顯示第一段加冒號加第七段。

[root@localhost awk]# awk -F ':' '{print $NR":"$NF}' test.txt 
rootx:/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
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/bin/bash
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/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
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/sbin/nologin
:/bin/bash
:/bin/bash
:/bin/bash
[root@localhost awk]#

把前三行的第一段改爲root

[root@localhost awk]# head -n3 /etc/passwd | 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]# 

上面的是賦值,前3行的第1段都變成了root。以前兩個=是匹配的意思,只能匹配出第1行。

[root@localhost awk]# head -n3 /etc/passwd | awk -F ':' '$1=="root"' 
root:x:0:0:root:/root:/bin/bash
[root@localhost awk]# 

[root@localhost awk]# head -n3 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

上面的分隔符沒有了,再用OFS定義一下就好了。
[root@localhost awk]# head -n3 test.txt | awk -F ':' '{OFS=":"} $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
[root@localhost awk]# 

求出全部行的第三段的和。tot是求和的意思,第一個大括號裏的是一個循環式,每行都循環一次,第一行就是0+第三段,第二行是第一行的第三段加第二行的第三段,以此類推。求和完畢之後,END結束,而後打印tot的和。

[root@localhost awk]# awk -F ':' '{(tot=tot+$3)}; END {print tot}' test.txt 
80049
[root@localhost awk]# 

總結:上面這些僅僅是正則表達式中最基本的內容,sed和awk並無深刻講解,但足以知足平常工做須要。碰到複雜的,再研究即是。

 

 

友情連接:阿銘Linux

相關文章
相關標籤/搜索