本章概要
[root@centos7-1 ~]#ll /usr/bin/awk 软链接指向gawk
lrwxrwxrwx. 1 root root 4 Jul 18 17:17 /usr/bin/awk -> gawk
awk语言
root:x:0:0:root:/root:/bin/bash
如果指定分隔符为冒号":",那么":"分割开的每一段内容即为一个字段;而该内容整行即为一条记录
awk工作原理
awk print格式
[root@centos7-1 ~]#awk '{print "hello awk"}'
aaa
hello awk
bbb
hello awk
vvv
hello awk
^C
[root@centos7-1 ~]#awk -F: '{print}' /etc/passwd {print}默认打印全部,这里只贴出部分内容
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
[root@centos7-1 ~]#awk -F: '{print "yuan"}' /etc/passwd /etc/passwd有多少行,就显示多少行yuan;空间原因,只贴出部分内容
yuan
yuan
yuan
yuan
yuan
yuan
yuan
[root@centos7-1 ~]#awk -F: '{print $1}' /etc/passwd 打印/etc/passwd每一条记录的第一个字段
root
bin
daemon
adm
lp
sync
[root@centos7-1 ~]#awk -F: '{print $0}' /etc/passwd 打印/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
[root@centos7-1 ~]#awk -F: '{print $1"\t"$3}' /etc/passwd 打印/etc/passwd每一条记录的第一个字段,第三个字段,中间以tab键分开
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
注意:\t在变量之间要用"",在普通字符串之间则不用""
如:[root@centos7-1 ~]#awk -F: '{print "hello\twak"}' /etc/passwd
hello wak
hello wak
hello wak
hello wak
hello wak
hello wak
[root@centos7-1 ~]#grep "^UUID" /etc/fstab |awk '{print $2,$4}'
/ defaults
/boot defaults
/data defaults
swap defaults
内置变量
[root@centos7-1 ~]#awk -v FS=':' '{print $1,FS,$3}' /etc/passwd
root : 0
bin : 1
daemon : 2
adm : 3
lp : 4
sync : 5
[root@centos7-1 ~]#awk -F: '{print $1,$3,$7}' /etc/passwd
root 0 /bin/bash
bin 1 /sbin/nologin
daemon 2 /sbin/nologin
adm 3 /sbin/nologin
[root@centos7-1 ~]#awk -v FS=':' -v OFS=':' '{print $1,$3,$7}' /etc/passwd
root:0:/bin/bash
bin:1:/sbin/nologin
daemon:2:/sbin/nologin
adm:3:/sbin/nologin
[root@centos7-1 ~]#awk -v RS=' ' '{print }' /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
[root@centos7-1 ~]#awk -v RS=' ' -v ORS='###' '{print }' /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/nologi
[root@centos7-1 ~]#awk -F: '{print NF}' /etc/passwd 引用变量时,变量前不需加$
7
7
7
7
[root@centos7-1 ~]#awk -F: '{print $(NF-1)}' /etc/passwd
/root
/bin
/sbin
/var/adm
/var/spool/lpd
[root@centos7-1 ~]#awk '{print NR}' /etc/passwd 打印所有记录号,这里只贴出部分
1
2
3
4
...
43
44
45
[root@centos7-1 ~]#awk '{print NR}' /etc/passwd /etc/group 会把两个文件内容叠加起来显示
1
2
3
4
...
119
120
121
[root@centos7-1 ~]#awk '{print FNR}' /etc/passwd /etc/group
1
2
3
4
...
44
45
1
2
3
4
...
74
75
[root@centos7-1 ~]#awk '{print FILENAME}' /etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
[root@centos7-1 ~]#awk '{print ARGC}' /etc/fstab /etc/passwd /etc/group
4
4
4
4
4
注意:除了/etc/fstab /etc/passwd /etc/group,awk本身也算是一个参数,因此参数个数为4
[root@centos7-1 ~]#awk 'BEGIN{print ARGV[0]}' /etc/fstab /etc/passwd
awk
[root@centos7-1 ~]#awk 'BEGIN{print ARGV[1]}' /etc/fstab /etc/passwd
/etc/fstab
[root@centos7-1 ~]#awk 'BEGIN{print ARGV[2]}' /etc/fstab /etc/passwd
/etc/passwd
自定义变量
awk -v test='hello gawk' '{print test}' /etc/fstab
awk -v test='hello gawk' 'BEGIN{print test}'
awk 'BEGIN{test="hello,gawk";print test}'
awk -F: '{sex="male";print $1,sex,age;age=18}' /etc/passwd
[root@centos7-1 ~]#cat awkscript
{print script,$1,$3}
[root@centos7-1 ~]#awk -F: -f awkscript /etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
printf命令
[root@centos7-1 ~]#awk -F: '{printf "%-20s %10d\n",$1,$3}' /etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
[root@centos7-1 ~]#awk -F: '{printf "username: %s\n",$1}' /etc/passwd
username: root
username: bin
username: daemon
username: adm
username: lp
username: sync
[root@centos7-1 ~]#awk -F: '{printf "username: %s UID:%d\n",$1,$3}' /etc/passwd
username: root UID:0
username: bin UID:1
username: daemon UID:2
username: adm UID:3
username: lp UID:4
username: sync UID:5
[root@centos7-1 ~]#awk -F: '{printf "username: %25s UID:%d\n",$1,$3}' /etc/passwd
username: root UID:0
username: bin UID:1
username: daemon UID:2
username: adm UID:3
username: lp UID:4
[root@centos7-1 ~]#awk -F: '{printf "username: %-25s UID:%d\n",$1,$3}' /etc/passwd
username: root UID:0
username: bin UID:1
username: daemon UID:2
username: adm UID:3
username: lp UID:4
username: sync UID:5
知识扩展:
使用awk制作表格:
awk -F: 'BEGIN{print "|username |userid |\n------------------------------------------"}{printf "%-30s|%-10d|\n------------------------------------------\n",$1,$3}' /etc/passwd > userlist
[root@centos7-1 ~]#cat userlist
|username |userid |
------------------------------------------
root |0 |
------------------------------------------
bin |1 |
------------------------------------------
daemon |2 |
------------------------------------------
adm |3 |
------------------------------------------
lp |4 |
------------------------------------------
sync |5 |
------------------------------------------
shutdown |6 |
------------------------------------------
=, +=, -=, *=, /=, %=, ^=,++, --
[root@centos7-1 ~]#awk 'BEGIN{i=0;print ++i,i}' 先做++运算,再打印i
1 1
[root@centos7-1 ~]#awk 'BEGIN{i=0;print i++,i}' 先打印i,再做++运算
0 1
[root@centos7-1 ~]#awk -F: '$0 ~ /root/{print $1}' /etc/passwd
root
[root@centos7-1 ~]#awk '$0~"^root"' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos7-1 ~]#awk '$0 !~ /root/' /etc/passwd 打印不包含root的行,这里只贴出部分内容
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
[root@centos7-1 ~]#awk -F: '$3==0' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos7-1 ~]#awk -F: '$0 ~ /root/{print $1}' /etc/passwd
root
[root@centos7-1 ~]#awk '$0~"^root"' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos7-1 ~]#awk '$0 !~ /root/' /etc/passwd 打印所有内容,这里只贴出部分内容
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
[root@centos7-1 ~]#awk -F: '$3==0' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos7-1 ~]#awk -F: '{$3>=1000?usertype="Common User":usertype=" SysUser";printf "%15s:%-s\n",$1,usertype}' /etc/passwd
root: SysUser
bin: SysUser
daemon: SysUser
adm: SysUser
lp: SysUser
awk PATTERN
[root@centos7-1 ~]#awk '/^UUID/{print $1}' /etc/fstab
UUID=2f1bda24-d0a0-4498-84b2-76ecad8602b8
UUID=da6027b7-04a8-412e-b54d-0a3cbe7a9e0d
UUID=13feb009-a4b1-4521-b6fe-2596f033f6a5
UUID=bbbfb1b0-b076-4203-a4d3-0b7cd755019d
[root@centos7-1 ~]#awk '!/^UUID/{print $1}' /etc/fstab 默认以空格为分隔符,打印第一列为#
#
#
#
#
#
#
#
(3) relational expression: 关系表达式,结果为“真”才会被处理
真:结果为非0值,非空字符串
假:结果为空字符串或0值
[root@centos7-1 ~]#awk -F: 'i=1;j=1{print i,j}' /etc/passwd 这里只贴出部分内容
root:x:0:0:root:/root:/bin/bash
1 1
bin:x:1:1:bin:/bin:/sbin/nologin
1 1
daemon:x:2:2:daemon:/sbin:/sbin/nologin
1 1
adm:x:3:4:adm:/var/adm:/sbin/nologin
awk ‘!0’ /etc/passwd ; awk ‘!1’ /etc/passwd
Awk -F: '$3>=1000{print $1,$3}' /etc/passwd
awk -F: '$3<1000{print $1,$3}' /etc/passwd
awk -F: '$NF=="/bin/bash"{print $1,$NF}' /etc/passwd
awk -F: '$NF ~ /bash$/{print $1,$NF}' /etc/passwd
[root@centos7-1 ~]#awk -F: '/^root\>/,/^nobody\>/{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
[root@centos7-1 ~]#awk -F: '(NR>=10&&NR<=20){print NR,$1}' /etc/passwd
10 operator
11 games
12 ftp
13 nobody
14 systemd-network
15 dbus
16 polkitd
17 sssd
18 libstoragemgmt
19 rpc
20 colord
awk -F : 'BEGIN {print "USER USERID"} {print $1":"$3} END{print "end file"}' /etc/passwd
awk -F : '{print "USER USERID“;print $1":"$3} END{print "end file"}' /etc/passwd
awk -F: 'BEGIN{print " USER UID \n--------------- "}{print $1,$3}' /etc/passwd
awk -F: 'BEGIN{print " USER UID \n--------------- "}{print $1,$3}'END{print "=============="} /etc/passwd
seq 10 |awk 'i=0'
seq 10 |awk 'i=1'
seq 10 | awk 'i=!i'
seq 10 | awk '{i=!i;print i}''
seq 10 | awk '!(i=!i)'
seq 10 |awk -v i=1 'i=!i'
知识扩展:
取分区利用率
df|awk -F" +|%" '/^\/dev\/sd/{print $1,$5}'
查看fstab中文件系统类别
cat /etc/fstab |awk '/^UUID/{print $3}'|sort|uniq -c|sort -nr
统计连接数,并且从大到小排序
netstat -nt|awk -F" +|:" '/^tcp/{print $6}'|sort|uniq -c|sort -nr
awk action
awk控制语句if-else
[root@centos7-1 ~]#awk -F: '{if($3>=1000)print $1,$3}' /etc/passwd
nfsnobody 65534
yuan 1000
wang 1001
[root@centos7-1 ~]#awk -F: '{if($NF=="/bin/bash") print $1}' /etc/passwd
root
wang
awk '{if(NF>5) print $0}' /etc/fstab
awk -F: '{if($3>=1000) {printf "Common user: %s\n",$1} else {printf "root or Sysuser: %s\n",$1}}' /etc/passwd
awk -F: '{if($3>=1000) printf "Common user: %s\n",$1; else printf "root or Sysuser: %s\n",$1}' /etc/passwd
df -h|awk -F% '/^\/dev/{print $1}'|awk '$NF>=80{print $1,$5}'
awk 'BEGIN{ test=100;if(test>90){print "very good"}
else if(test>60){ print "good"}else{print "no pass"}}'
知识扩展:
找出访问次数超过10000次的ip地址,并放入防火墙
cat access_log | awk '{print $1}'|sort|uniq -c|sort -nr|awk '{if($1>10000)print $2}'|while read ip ;do iptables -A -s $ip -j REJECT;done
awk控制语句while循环
列出linux16这一行每个单词出现的次数
[root@centos7-1 ~]#awk '/^[[:space:]]*linux16/{i=1;while(i<=NF){print $i,length($i); i++}}' /etc/grub2.cfg
linux16 7
/vmlinuz-3.10.0-862.el7.x86_64 30
root=UUID=2f1bda24-d0a0-4498-84b2-76ecad8602b8 46
ro 2
rhgb 4
quiet 5
net.ifnames=0 13
linux16 7
/vmlinuz-0-rescue-7eab25876df747da952cee5f5a6dbc5f 50
root=UUID=2f1bda24-d0a0-4498-84b2-76ecad8602b8 46
ro 2
rhgb 4
quiet 5
net.ifnames=0 13
构建100个随机数,取出最大值和最小值
[root@centos7-1 ~]#awk -F"," '{max=$1;min=$1;i=2;while(i<=NF){if($i>max){max=$i}else if($9
awk控制语句do-while循环
[root@centos7-1 ~]#awk 'BEGIN{ total=0;i=0;do{ total+=i;i++;}while(i<=100);print total}'
5050
awk控制语句for循环
[root@centos7-1 ~]#awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}' /etc/grub2.cfg
linux16 7
/vmlinuz-3.10.0-862.el7.x86_64 30
root=UUID=2f1bda24-d0a0-4498-84b2-76ecad8602b8 46
ro 2
rhgb 4
quiet 5
net.ifnames=0 13
linux16 7
/vmlinuz-0-rescue-7eab25876df747da952cee5f5a6dbc5f 50
root=UUID=2f1bda24-d0a0-4498-84b2-76ecad8602b8 46
ro 2
rhgb 4
quiet 5
net.ifnames=0 13
性能比较
[root@centos7-1 ~]#time (awk 'BEGIN{ total=0;for(i=0;i<=10000;i++){total+=i;};print total;}')
50005000
real 0m0.004s
user 0m0.003s
sys 0m0.001s
[root@centos7-1 ~]#time(total=0;for i in {1..10000};do total=$(($total+i));done;echo $total)
50005000
real 0m0.059s
user 0m0.044s
sys 0m0.015s
[root@centos7-1 ~]#time(for ((i=0;i<=10000;i++));do let total+=i;done;echo $total)
50005000
real 0m0.081s
user 0m0.070s
sys 0m0.011s
[root@centos7-1 ~]#time(seq –s ”+” 10000|bc)
seq: invalid floating point argument: –s
Try 'seq --help' for more information.
real 0m0.344s
user 0m0.000s
sys 0m0.009s
awk控制语句switch语句、break、continue、next
awk 'BEGIN{sum=0;for(i=1;i<=100;i++){if(i%2==0)continue;sum+=i}print sum}''
awk 'BEGIN{sum=0;for(i=1;i<=100;i++){if(i==66)break;sum+=i}print sum}''
扩展用法:
break [n]
continue [n]
[root@centos7-1 ~]#awk -F: '{if($3%2!=0) next; print NR,$1,$3}' /etc/passwd 打印奇数行,这里只贴出部分内容
1 root 0
3 daemon 2
5 lp 4
7 shutdown 6
9 mail 8
11 games 12
12 ftp 14
14 systemd-network 192
17 sssd 998
1到100之间的偶数相加
[root@centos7-1 ~]#awk 'BEGIN{sum=0;for(i=1;i<=100;i++){if(i%2==1)continue; sum+=i};print sum}'
2550
weekdays["mon"]="Monday"
awk 'BEGIN{weekdays["mon"]="Monday";
weekdays["tue"]="Tuesday";print weekdays["mon"]}'
[root@centos7-1 ~]#cat f2.txt
aaa
bbb
aaa
ccc
ddd
aaa
ccc
[root@centos7-1 ~]#awk '!arr[$0]++' f2.txt 给文件“去重”
aaa
bbb
ccc
ddd
[root@centos7-1 ~]#awk '{!arr[$0]++;print $0, arr[$0]}' f2.txt
aaa 1
bbb 1
aaa 2
ccc 1
ddd 1
aaa 3
ccc 2
[root@centos7-1 ~]#awk 'BEGIN{weekdays["mon"]="Monday";weekdays["tue"]="Tuesday";for(i in weekdays) {print weekdays[i]}}'
Tuesday
Monday
[root@centos7-1 ~]#netstat -tan | awk '/^tcp/{state[$NF]++}END{for(i in state) { print i,state[i]}}'
LISTEN 18
ESTABLISHED 2
[root@centos7-1 ~]#awk '{ip[$1]++}END{for(i in ip) {print i,ip[i]}}' access_log
172.20.129.234 407
172.20.120.185 195
172.20.130.5 1206
172.20.59.26 55
172.20.32.7 100
172.20.129.253 40
172.20.0.7 20
172.20.112.128 33
172.20.12.14 10
172.20.129.178 20
172.20.129.246 220
取出fqdn,并排序;fqdn即域名(mail.magedu.com)
[root@centos7-1 ~]#cat f3.txt
http://mail.magedu.com/index.html
http://www.magedu.com/test.html
http://study.magedu.com/index.html
http://blog.magedu.com/index.html
http://www.magedu.com/images/logo.jpg
http://blog.magedu.com/20080102.html
[root@centos7-1 ~]#cat f3.txt |awk -F"/" '{url[$3]++}END{for(i in url){print url[i],i}}'|sort -nr
2 www.magedu.com
2 blog.magedu.com
1 study.magedu.com
1 mail.magedu.com
综合用法:
1、求男生(性别为m),女生(性别f)的平均成绩
[root@centos7-1 ~]#cat score.txt
name score sex
mage 100 m
zhang 99 f
li 99 f
zhao 98 f
wang 90 m
方法1:用四个变量
男生成绩score_m
女生成绩score_f
男生数量num_m
女生数量num_f
[root@centos7-1 ~]#cat score.txt|awk '!/^name/{if($3=="m"){score_m+=$2;num_m++}else{score_f+=$2;num_f++}}END{print "avg_m="score_m/num_m,"avg_f="score_f/num_f}'
avg_m=96.3333 avg_f=98.5
方法2:数组
[root@centos7-1 ~]#awk 'NR!=1{score[$3]+=$2;num[$3]++}END{for(sex in num){print sex":avg="score[sex]/num[sex]}}' score.txt
m:avg=96.3333
f:avg=98.5
2、求/etc/fstab每个单词出现的个数
cat /etc/fstab |awk '{for(i=1;i<=NF;i++)word[$i]++}END{for (i in word){print i,word[i]}}'
[root@centos7-1 ~]#awk 'BEGIN{srand(); for (i=1;i<=10;i++)print int(rand()*100) }'
65
87
65
59
12
59
52
51
22
28
[root@centos7-1 ~]#echo "2008:08:08 08:08:08" |awk 'sub(/:/,"-",$1)'
2008-08:08 08:08:08
gsub(r,s,[t]):对t字符串进行搜索r表示的模式匹配的内容,并全部替换为s所表示的内容
[root@centos7-1 ~]#echo "2008:08:08 08:08:08" |awk 'gsub(/:/,"-",$1)'
2008-08-08 08:08:08
split(s,array,[r]):以r为分隔符,切割字符串s,并将切割后的结果保存至array所表示的数组中,第一个索引值为1,第二个索引值为2,…
[root@centos7-1 ~]#netstat -tn | awk '/^tcp\>/{split($5,ip,":");count[ip[1]]++}END{for (i in count) {print i,count[i]}}'
192.168.32.1 2
[root@centos7-1 ~]#cat fun.awk
function max(x,y) {
x>y?var=x:var=y
return var
}
BEGIN{a=3;b=2;print max(a,b)}
[root@centos7-1 ~]#awk -f f1
3
awk中调用shell命令
awk脚本
cat f1.awk
{if($3>=1000)print $1,$3}
awk -F: -f f1.awk /etc/passwd
cat f2.awk
#!/bin/awk –f
#this is a awk script
{if($3>=1000)print $1,$3}
chmod +x f2.awk
f2.awk –F: /etc/passwd
向awk脚本传递参数
cat test.awk
#!/bin/awk –f
{if($3 >=min && $3<=max)print $1,$3}
chmod +x test.awk
test.awk -F: min=100 max=200 /etc/passwd