简单介绍shell编程四剑客之awk
概要:分别的作用
grep:文本过滤(模式:pattern)工具,grep,egrep,fgrep,擅长过滤。
sed:stream editor 文本编辑工具;(流编辑器),擅长取行、替换。
awk:linux上的实现gawk,文件报告生成器;(独立的编程语言),过滤内容,擅长取列。
find: linux上实时查找工具,通过便利指定路径下得文件系统完成文件查找。
一、awk
awk能够进行文本处理和报表生产,经常用在linux日常处理的工作中
1、语法参数格式为:awk 'pattern + {action}' file
awk 选项 模式 动作 文件
2、语法详解
单引号时和shell命令区分开;
大括号{}表示一个命令分组;
pattern是要给过滤器,表示匹配pattern条件的行才进行action处理;
action是处理动作,常见动作为print
pattren和action可以只有其一,但不能都没有
3、选项
-F 指定输入时用到的字段分隔符
-v var=value:自定义变量
4、模式介绍
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
5、动作
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
6、变量
$0所有内容,$1第一列,$2第二列,$NF最后一列,$FN-1倒数第二列
各种变量如下:
7、运算符号
实例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
拆解一下具体的变量如下:
实例2、打印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]#
实例3、打印第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
实例4、打印机奇偶数行
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]#
实例4、打印字段数大于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]#
实例5、打印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]#
实例6、打印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
实例7、计算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]#
实例8、找出普通用户和用户名并统计数量
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]#
转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/10743376.html