六周第三次课

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

六周第三次课

 9.6/9.7 awk 

awk也是流式编辑器,针对文档中的行来操作,一行一行地执行。

 

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

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

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

 

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

这种打印也不是删除文本的某些段,相当于sed不加-i选项的作用,下面打印test.txtt的所有段。

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

 

awk可以直接打印全部文本。

[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

转载于:https://my.oschina.net/u/3744518/blog/1608818

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391425.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

macaca web(4)

米西米西滴&#xff0c;吃过中午饭来一篇&#xff0c;话说&#xff0c;上回书说道macaca 测试web&#xff08;3&#xff09;&#xff0c;参数驱动来搞&#xff0c;那么有小伙本又来给雷子来需求&#xff0c; 登录模块能不能给我给重新封装一下吗&#xff0c; 我说干嘛封装&…

rfm模型分析与客户细分_如何使用基于RFM的细分来确定最佳客户

rfm模型分析与客户细分With some free time at hand in the midst of COVID-19 pandemic, I decided to do pro bono consulting work. I was helping a few e-commerce companies with analyzing their customer data. A common theme I encountered during this work was tha…

数据仓库项目分析_数据分析项目:仓库库存

数据仓库项目分析The code for this project can be found at my GitHub.该项目的代码可以在我的GitHub上找到 。 介绍 (Introduction) The goal of this project was to analyse historic stock/inventory data to decide how much stock of each item a retailer should hol…

web前端效率提升之浏览器与本地文件的映射-遁地龙卷风

1.chrome浏览器&#xff0c;机制是拦截url&#xff0c;      1.在浏览器Element中调节的css样式可以直接同步到本地文件&#xff0c;反之亦然&#xff0c;浏览器会重新加载css&#xff0c;省去刷新   2.在source面板下对js的编辑可以同步到本地文件&#xff0c;反之亦然…

归因分析_归因分析:如何衡量影响? (第2部分,共2部分)

归因分析By Lisa Cohen, Ryan Bouchard, Jane Huang, Daniel Yehdego and Siddharth Kumar由 丽莎科恩 &#xff0c; 瑞安布沙尔 &#xff0c; 黄美珍 &#xff0c; 丹尼尔Yehdego 和 亚洲时报Siddharth库马尔 介绍 (Introduction) This is our second article in a series wh…

linux与磁盘相关的内容

本节所讲内容1.认识SAS-SATA-SSD-SCSI-IDE硬盘2.使用fdisk对磁盘进行操作&#xff0c;分区&#xff0c;格式化3.开机自动挂载分区4.使用parted操作大于等于4T硬盘5.扩展服务器swap内存空间 MBR(Master Boot Record)主引导记录&#xff0c;也就是现有的硬盘分区模式。MBR分区的标…

页面布局

页面布局两大类&#xff1a;   主站&#xff1a; 1 <div classpg-header> 2 <div stylewidth:980px;margin:0 auto;> 3 内容自动居中 4 </div> 5 <div classpg-content></div> 6 <div classpg-footer></div&…

sonar:默认的扫描规则

https://blog.csdn.net/liumiaocn/article/details/83550309 https://note.youdao.com/ynoteshare1/index.html?id3c1e6a08a21ada4dfe0123281637e299&typenote https://blog.csdn.net/liumiaocn/article/details/83550309 文本版&#xff1a; soanr规则java版 …

多变量线性相关分析_如何测量多个变量之间的“非线性相关性”?

多变量线性相关分析现实世界中的数据科学 (Data Science in the Real World) This article aims to present two ways of calculating non linear correlation between any number of discrete variables. The objective for a data analysis project is twofold : on the one …

探索性数据分析(EDA):Python

什么是探索性数据分析(EDA)&#xff1f; (What is Exploratory Data Analysis(EDA)?) If we want to explain EDA in simple terms, it means trying to understand the given data much better, so that we can make some sense out of it.如果我们想用简单的术语来解释EDA&a…

微服务框架---搭建 go-micro环境

1.安装micro 需要使用GO1.11以上版本 #linux 下 export GO111MODULEon export GOPROXYhttps://goproxy.io # windows下设置如下环境变量 setx GO111MODULE on setx GOPROXY https://goproxy.io # 使用如下指令安装 go get -u -v github.com/micro/micro go get -u -v github.co…

写作工具_4种加快数据科学写作速度的工具

写作工具I’ve been writing about data science on Medium for just over two years. Writing, in particular, technical writing can be time-consuming. Not only do you need to come up with an idea, write well, edit your articles for accuracy and flow, and proofr…

python数据结构与算法

2019独角兽企业重金招聘Python工程师标准>>> http://python.jobbole.com/tag/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/ 转载于:https://my.oschina.net/u/3572879/blog/1611369

大数据(big data)_如何使用Big Query&Data Studio处理和可视化Google Cloud上的财务数据...

大数据(big data)介绍 (Introduction) This article will show you one of the ways you can process stock price data using Google Cloud Platform’s BigQuery, and build a simple dashboard on the processed data using Google Data Studio.本文将向您展示使用Google Cl…

ubuntu 16.04常用命令

ip配置&#xff1a; 终端输入vi /etc/network/interfaces命令编辑配置文件,增加如下内容&#xff1a;         auto enp2s0    iface enp2s0 inet static    address 192.168.1.211    netmask 255.255.255.0    gateway 192.168.1.1 重启网卡&#xf…

多元时间序列回归模型_多元时间序列分析和预测:将向量自回归(VAR)模型应用于实际的多元数据集...

多元时间序列回归模型Multivariate Time Series Analysis多元时间序列分析 A univariate time series data contains only one single time-dependent variable while a multivariate time series data consists of multiple time-dependent variables. We generally use mult…

字符串基本操作

1.已知‘星期一星期二星期三星期四星期五星期六星期日 ’&#xff0c;输入数字&#xff08;1-7&#xff09;&#xff0c;输出相应的‘星期几 s星期一星期二星期三星期四星期五星期六星期日 d int(input(输入1-7:)) print(s[3*(d-1):3*d]) 2.输入学号&#xff0c;识别年级、专业…

数据分析和大数据哪个更吃香_处理数据,大数据甚至更大数据的17种策略

数据分析和大数据哪个更吃香Dealing with big data can be tricky. No one likes out of memory errors. ☹️ No one likes waiting for code to run. ⏳ No one likes leaving Python. &#x1f40d;处理大数据可能很棘手。 没有人喜欢内存不足错误。 No️没有人喜欢等待代码…

MySQL 数据还原

1.1还原使用mysqldump命令备份的数据库的语法如下&#xff1a; mysql -u root -p [dbname] < backup.sq 示例&#xff1a; mysql -u root -p < C:\backup.sql 1.2还原直接复制目录的备份 通过这种方式还原时&#xff0c;必须保证两个MySQL数据库的版本号是相同的。MyISAM…

VueJs学习入门指引

新产品开发决定要用到vuejs&#xff0c;总结一个vuejs学习指引。 1.安装一个Node环境 去Nodejs官网下载windows版本node 下载地址&#xff1a; https://nodejs.org/zh-cn/ 2.使用node的npm工具搭建一个Vue项目&#xff0c;这里混合进入了ElementUI 搭建指引地址: https:…