awk


awk

  • 简单用法,打印以:为分割的第一段

[root@localhost awk]# awk -F ':' '{print $1}' test.txt 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-bus-proxy
systemd-network
dbus
polkitd
tss
postfix
sshd
chrony
user1
user2
user3
[root@localhost 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
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/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
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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:997:995::/var/lib/chrony:/sbin/nologin
user1:x:1000:1007::/home/user1:/bin/bash
user2:x:1001:1008::/home/user2:/bin/bash
user3:x:1005:1000::/home/user3:/bin/bash
[root@localhost awk]# 
  • 列出多个段

    [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
    systemd-bus-proxy 999 997
    systemd-network 192 192
    dbus 81 81
    polkitd 998 996
    tss 59 59
    postfix 89 89
    sshd 74 74
    chrony 997 995
    user1 1000 1007
    user2 1001 1008
    user3 1005 1000
    [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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@localhost awk]# 
  • 只匹配第一行有oo的行

[root@localhost awk]# awk -F ':' '$1 ~ /oo/' test.txt 
root:x:0:0:root:/root:/bin/bash
[root@localhost awk]# 
  • 同样支持正则表达式

[root@localhost awk]# awk -F ':' '$1 ~ /oo+/' test.txt 
root:x:0:0:root:/root:/bin/bash

[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
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@localhost awk]# 
  • 同时匹配多个需求的行

[root@localhost awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt 
root 0
operator 11
tss 59 59
user1 1000 1007
user2 1001 1008
user3 1005 1000
[root@localhost awk]# 
  • 匹配指定一个数字范围

    ==两个等于号是等于的意思,因为一个等于号会误认为是赋值
    数值不能用双引号

[root@localhost awk]# awk -F ':' '$3==0' test.txt 
root:x:0:0:root:/root:/bin/bash
[root@localhost awk]# awk -F ':' '$3==0 {print $1}' test.txt 
root
[root@localhost awk]# awk -F ':' '$3>=1000 {print $1}' test.txt 
user1
user2
user3
[root@localhost awk]# awk -F ':' '$3>=1000 {print $0}' test.txt 
user1:x:1000:1007::/home/user1:/bin/bash
user2:x:1001:1008::/home/user2:/bin/bash
user3:x:1005:1000::/home/user3:/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
user1:x:1000:1007::/home/user1:/bin/bash
user2:x:1001:1008::/home/user2:/bin/bash
user3:x:1005:1000::/home/user3:/bin/bash
[root@localhost awk]# 

列出第三段小于第四段

[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
user1:x:1000:1007::/home/user1:/bin/bash
user2:x:1001:1008::/home/user2:/bin/bash
[root@localhost awk]# 

列出第三段数值等于第四段

[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
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost awk]# 

列出第三段大于5小于7的字段

[root@localhost awk]# awk -F ':' '$3>"5" && $3<"7"' test.txt 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

匹配第三段数值大于1000或者第七段是/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
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/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
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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:997:995::/var/lib/chrony:/sbin/nologin
user2:x:1001:1008::/home/user2:/bin/bash
user3:x:1005:1000::/home/user3:/bin/bash
[root@localhost awk]# 

列出第三段大于1000或者第七段含有bash字段

[root@localhost awk]# awk -F ':' '$3>1000 || $7 ~ /bash/' test.txt 
root:x:0:0:root:/root:/bin/bash
user1:x:1000:1007::/home/user1:/bin/bash
user2:x:1001:1008::/home/user2:/bin/bash
user3:x:1005:1000::/home/user3:/bin/bash
[root@localhost awk]# 
  • 指定分隔符

    语法:{OFS=""} 条件 {打印内容}

[root@localhost awk]# awk -F ':' '{OFS="#"} $3>1000 || $7 ~ /bash/ {print $1,$3,$7} ' test.txt 
root#0#/bin/bash
user1#1000#/bin/bash
user2#1001#/bin/bash
user3#1005#/bin/bash
[root@localhost awk]# 
  • 显示行号

[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:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16:dbus:x:81:81:System message bus:/:/sbin/nologin
17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin
22:user1:x:1000:1007::/home/user1:/bin/bash
23:user2:x:1001:1008::/home/user2:/bin/bash
24:user3:x:1005:1000::/home/user3:/bin/bash
[root@localhost awk]# 
  • 显示段号

当然这里面都是7段,如果删除一个:,前面段数就变成了6

[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:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/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:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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:997:995::/var/lib/chrony:/sbin/nologin
7:user1:x:1000:1007::/home/user1:/bin/bash
7:user2:x:1001:1008::/home/user2:/bin/bash
7:user3:x:1005:1000::/home/user3:/bin/bash
[root@localhost awk]# 
  • 打印前10行

[root@localhost awk]# awk -F ':' 'NR<=10' test.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost awk]# 
  • 在前10行里面找到第一段含有root或者sync的

[root@localhost awk]# awk -F ':' 'NR<=10 && $1 ~ /root|sync/' test.txt 
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
[root@localhost awk]#