簡單介紹shell編程四劍客之awk

概要:分別的做用html

grep:文本過濾(模式:pattern)工具,grep,egrep,fgrep,擅長過濾。mysql

sed:stream editor 文本編輯工具;(流編輯器),擅長取行、替換。linux

awk:linux上的實現gawk,文件報告生成器;(獨立的編程語言),過濾內容,擅長取列。nginx

find: linux上實時查找工具,經過便利指定路徑下得文件系統完成文件查找。web

 

1、awk

  awk可以進行文本處理和報表生產,常常用在linux平常處理的工做中sql

  一、語法參數格式爲:awk 'pattern + {action}' fileshell

    awk  選項 模式 動做 文件express

  二、語法詳解編程

       單引號時和shell命令區分開;centos

       大括號{}表示一個命令分組;

       pattern是要給過濾器,表示匹配pattern條件的行才進行action處理;

       action是處理動做,常見動做爲print

       pattren和action能夠只有其一,但不能都沒有

  三、選項  

    -F 指定輸入時用到的字段分隔符

    

    -v var=value:自定義變量

     

    

    

  四、模式介紹
     1)、BEGIN{ statements } 。
      讓用戶指定在第一條輸入記錄被處理以前所發生的動做,一般可在這裏設置全局變量
      在輸入被讀取以前,statements執行一次
     2)、END{ statements }
      讓用戶在最後一條輸入記錄被讀取以後發生的動做
      當全部輸入被讀取完畢以後,statements執行一次
     3)、expression{ statements }
      關係表達式,能夠用下面運算符表中的關係運算符進行操做,如%2>%1選擇第二個字段比第一個字段長的行。
      每碰到一個是expression爲真的輸入行,statements執行一次,expression爲真指的是其值非零或非空
     4)、/regular expression/ { statements }
      通配符的擴展集
      碰到這樣一個輸入行時,statements就執行:輸入行含由一段字符串,而該字符串能夠被regular expression匹配
     5)、compound pattern { statements }
      一個複合模式將表達式用&&(AND),(OR),!(NOT),以及括號組合起來;當compound pattern爲真時,statements執行。
     6)、pattern1,pattern2 { statements }
      模式,模式,指定一個行的範圍不能包括BEGIN和END模式
      一個範圍模式匹配多個輸入行,這些輸入行從匹配pattern1的行開始,到匹配pattern2的行結束(包括這兩行),對這其中的每一行執行statements

   五、動做
    expression表達式,包括常量,變量,賦值,函數調用等等 。
    print expression-list
    printf(format,expression-list)
    if(expression)statements
    if (expression)statements else statements
    while (expression) statements
    for (expression in array)statements
    for (expression in array) statements
    do statements while (expression)
    break
    contnue

   六、變量

    $0全部內容,$1第一列,$2第二列,$NF最後一列,$FN-1倒數第二列

    各類變量以下:

 

  七、運算符號

 

 

     實例一、awk '{print $0}' /etc/passwd  

[root@web2 keepalived]# awk '{print $0}' /etc/passwd
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

 

    拆解一下具體的變量以下:

    

    實例二、打印uid在30~40範圍內用戶名

1 [root@web2 keepalived]# awk -F: '$3>=30&&$3<=40{print $3}' /etc/passwd
2 32
3 38
4 [root@web2 keepalived]# 

    實例三、打印第5-10行的行號和用戶名

1 [root@web2 keepalived]# awk -F: 'NR>=5&&NR<=10{print $1,NR}' /etc/passwd
2 lp 5
3 sync 6
4 shutdown 7
5 halt 8
6 mail 9
7 operator 10

    實例四、打印機奇偶數行

 1 [root@web2 keepalived]# awk -F: 'NR%2==1{print NR,$0}' /etc/passwd
 2 1 root:x:0:0:root:/root:/bin/bash
 3 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 4 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 5 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
 6 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
 7 11 games:x:12:100:games:/usr/games:/sbin/nologin
 8 13 nobody:x:99:99:Nobody:/:/sbin/nologin
 9 15 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
10 17 polkitd:x:998:997:User for polkitd:/:/sbin/nologin
11 19 unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
12 21 libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
13 23 colord:x:995:993:User for colord:/var/lib/colord:/sbin/nologin
14 25 saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
15 27 rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
16 29 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
17 31 qemu:x:107:107:qemu user:/:/sbin/nologin
18 33 chrony:x:992:989::/var/lib/chrony:/sbin/nologin
19 35 sssd:x:990:987:User for sssd:/:/sbin/nologin
20 37 gdm:x:42:42::/var/lib/gdm:/sbin/nologin
21 39 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
22 41 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
23 43 zhangxingeng:x:1000:1000:centos-7-64:/home/zhangxingeng:/bin/bash
24 45 mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
25 [root@web2 keepalived]# awk -F: 'NR%2==0{print NR,$0}' /etc/passwd 
26 2 bin:x:1:1:bin:/bin:/sbin/nologin
27 4 adm:x:3:4:adm:/var/adm:/sbin/nologin
28 6 sync:x:5:0:sync:/sbin:/bin/sync
29 8 halt:x:7:0:halt:/sbin:/sbin/halt
30 10 operator:x:11:0:operator:/root:/sbin/nologin
31 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
32 14 systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin
33 16 dbus:x:81:81:System message bus:/:/sbin/nologin
34 18 abrt:x:173:173::/etc/abrt:/sbin/nologin
35 20 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
36 22 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
37 24 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
38 26 geoclue:x:993:991:User for geoclue:/var/lib/geoclue:/sbin/nologin
39 28 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
40 30 radvd:x:75:75:radvd user:/:/sbin/nologin
41 32 ntp:x:38:38::/etc/ntp:/sbin/nologin
42 34 setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin
43 36 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
44 38 gnome-initial-setup:x:989:984::/run/gnome-initial-setup/:/sbin/nologin
45 40 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
46 42 tcpdump:x:72:72::/:/sbin/nologin
47 44 nginx:x:988:983:Nginx web server:/var/lib/nginx:/sbin/nologin
48 [root@web2 keepalived]# 

    實例四、打印字段數大於5的行

 1 [root@web2 keepalived]# awk -F: 'NF>6{print $0}' /etc/passwd 
 2 root:x:0:0:root:/root:/bin/bash
 3 bin:x:1:1:bin:/bin:/sbin/nologin
 4 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 5 adm:x:3:4:adm:/var/adm:/sbin/nologin
 6 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 7 sync:x:5:0:sync:/sbin:/bin/sync
 8 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
 9 halt:x:7:0:halt:/sbin:/sbin/halt
10 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 operator:x:11:0:operator:/root:/sbin/nologin
12 games:x:12:100:games:/usr/games:/sbin/nologin
13 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
14 nobody:x:99:99:Nobody:/:/sbin/nologin
15 systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/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:997:User for polkitd:/:/sbin/nologin
19 abrt:x:173:173::/etc/abrt:/sbin/nologin
20 unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
21 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
22 libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
23 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
24 colord:x:995:993:User for colord:/var/lib/colord:/sbin/nologin
25 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
26 saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
27 geoclue:x:993:991:User for geoclue:/var/lib/geoclue:/sbin/nologin
28 rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
29 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
30 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
31 radvd:x:75:75:radvd user:/:/sbin/nologin
32 qemu:x:107:107:qemu user:/:/sbin/nologin
33 ntp:x:38:38::/etc/ntp:/sbin/nologin
34 chrony:x:992:989::/var/lib/chrony:/sbin/nologin
35 setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin
36 sssd:x:990:987:User for sssd:/:/sbin/nologin
37 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
38 gdm:x:42:42::/var/lib/gdm:/sbin/nologin
39 gnome-initial-setup:x:989:984::/run/gnome-initial-setup/:/sbin/nologin
40 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
41 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
42 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
43 tcpdump:x:72:72::/:/sbin/nologin
44 zhangxingeng:x:1000:1000:centos-7-64:/home/zhangxingeng:/bin/bash
45 nginx:x:988:983:Nginx web server:/var/lib/nginx:/sbin/nologin
46 mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
47 [root@web2 keepalived]# 

    實例五、打印UID不等於GID的用戶名

 1 [root@web2 keepalived]# awk -F: '$3!=$4{print $0}' /etc/passwd
 2 adm:x:3:4:adm:/var/adm:/sbin/nologin
 3 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 4 sync:x:5:0:sync:/sbin:/bin/sync
 5 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
 6 halt:x:7:0:halt:/sbin:/sbin/halt
 7 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
 8 operator:x:11:0:operator:/root:/sbin/nologin
 9 games:x:12:100:games:/usr/games:/sbin/nologin
10 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
11 systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin
12 polkitd:x:998:997:User for polkitd:/:/sbin/nologin
13 unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
14 libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
15 colord:x:995:993:User for colord:/var/lib/colord:/sbin/nologin
16 saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
17 geoclue:x:993:991:User for geoclue:/var/lib/geoclue:/sbin/nologin
18 chrony:x:992:989::/var/lib/chrony:/sbin/nologin
19 setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin
20 sssd:x:990:987:User for sssd:/:/sbin/nologin
21 gnome-initial-setup:x:989:984::/run/gnome-initial-setup/:/sbin/nologin
22 nginx:x:988:983:Nginx web server:/var/lib/nginx:/sbin/nologin
23 [root@web2 keepalived]# 

    實例六、打印1~100之內的7的倍數和包含7的數

 1 [root@web2 keepalived]# seq 1 100 | awk '$1~/7/||$1%7==0{print $1}'
 2 7
 3 14
 4 17
 5 21
 6 27
 7 28
 8 35
 9 37
10 42
11 47
12 49
13 56
14 57
15 63
16 67
17 70
18 71
19 72
20 73
21 74
22 75
23 76
24 77
25 78
26 79
27 84
28 87
29 91
30 97
31 98

    實例七、計算UID相加的總和;計算GID相加的總和

1 [root@web2 keepalived]# awk -F: '{uid+=$3;gid+=$4}END{print uid;print gid}' /etc/passwd
2 80244
3 79400
4 [root@web2 keepalived]# 

    實例八、找出普通用戶和用戶名並統計數量

      

1 [root@web2 keepalived]#  awk -F: '{if($3>=500) sum+=1}END{print sum}' /etc/passwd
2 14
3 [root@web2 keepalived]# 

      uid分組標記爲:0 admin,1-499 sysuser,500 + users

 1 [root@web2 keepalived]# awk 'BEGIN{FS=":";OFS="\t";print "用戶名\tUID\t權限"}{if($3==0)print $1,$3,"admin";else if($3<500)print $1,
 2 > $3,"sysusers";else print $1,$3,"users"}' /etc/passwd
 3 用戶名  UID     權限
 4 root    0       admin
 5 bin     1       sysusers
 6 daemon  2       sysusers
 7 adm     3       sysusers
 8 lp      4       sysusers
 9 sync    5       sysusers
10 shutdown        6       sysusers
11 halt    7       sysusers
12 mail    8       sysusers
13 operator        11      sysusers
14 games   12      sysusers
15 ftp     14      sysusers
16 nobody  99      sysusers
17 systemd-bus-proxy       999     users
18 systemd-network 192     sysusers
19 dbus    81      sysusers
20 polkitd 998     users
21 abrt    173     sysusers
22 unbound 997     users
23 tss     59      sysusers
24 libstoragemgmt  996     users
25 rpc     32      sysusers
26 colord  995     users
27 usbmuxd 113     sysusers
28 saslauth        994     users
29 geoclue 993     users
30 rtkit   172     sysusers
31 rpcuser 29      sysusers
32 nfsnobody       65534   users
33 radvd   75      sysusers
34 qemu    107     sysusers
35 ntp     38      sysusers
36 chrony  992     users
37 setroubleshoot  991     users
38 sssd    990     users
39 pulse   171     sysusers
40 gdm     42      sysusers
41 gnome-initial-setup     989     users
42 sshd    74      sysusers
43 avahi   70      sysusers
44 postfix 89      sysusers
45 tcpdump 72      sysusers
46 zhangxingeng    1000    users
47 nginx   988     users
48 mysql   27      sysusers
49 [root@web2 keepalived]# 

 

    

 

轉載請註明出處:http://www.javashuo.com/article/p-zhoesfmu-ge.html 

相關文章
相關標籤/搜索