awk的使用
awk的处理方式与格式
awk的处理方式
- awk一次处理一行内容
- awk可以对每行进行切片处理
示例:
awk '{print $1}'
awk的格式
命令行格式
awk [options] 'command' files
command1:pattern {awk操作命令}
操作命令:
- 内置函数:print(),printf(),getline…
- 控制指令:if(){…}else{…},while()
脚本格式
awk -f awk-script-file file(s)
扩展格式
BEGIN{…}{…}END{…}
示例:
以制表的形式输出passwd
文件中的NR,NF,User
awk -F ':' 'BEGIN{print "Line\t Columns User"}{print NR"\t",NF"\t",$1}END{print "-------"FILENAME"-------"}' passwd
结果:
Line Columns User
1 7 root
2 7 bin
3 7 daemon
4 7 adm
5 7 lp
6 7 sync
7 7 shutdown
8 7 halt
9 7 mail
10 7 operator
11 7 games
12 7 ftp
13 7 nobody
14 7 systemd-network
15 7 dbus
16 7 polkitd
17 7 abrt
18 7 tss
19 7 postfix
20 7 chrony
21 7 sshd
22 7 mysql
-------passwd-------
awk的内置参数
变量
-
$0 : 表示整个当前行
-
$1 : 表示第一个字段
-
$2 : 每行第二个字段
以此类推 -
NR : 每行的记录号(行号)
-
NF : 字段数(以分隔符为参照物)
-
FILENAME : 正在处理的文件名
示例:
awk -F ':' '{print NR,NF}' /etc/passwd
结果:
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7
11 7
12 7
13 7
14 7
15 7
16 7
17 7
18 7
19 7
20 7
21 7
22 7
awk -F ':' '{print FILENAME}' /etc/passwd
结果:
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
/etc/passwd
- ~ : 对变量进行更加详细的匹配
示例:
输出passwd
文件中以a-c
开头的用户名
awk -F ':' '$1~/^[a-c].*/{print $1}' passwd
结果:
bin
adm
abrt
chrony
输出passwd
文件中不以a-c
开头的用户名
awk -F ':' '$1!~/^[a-c].*/{print $1}' passwd
结果:
root
daemon
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
tss
postfix
sshd
mysql
- “>,<,==,!=” : 逻辑判断
示例:
输出passwd
文件中UID
大于80的用户名和UID
awk -F ':' '$3>80{print $1,$3}' passwd
结果:
nobody 99
systemd-network 192
dbus 81
polkitd 999
abrt 173
postfix 89
chrony 998
分隔符
options: -F field-separator(默认为空格)
示例:
打印passwd
中的组id
awk -F ':' '{print $3}' /etc/passwd
结果:
0
1
2
3
4
5
6
7
8
11
12
14
99
192
81
999
173
59
89
998
74
27
打印passwd
中的用户名
和组id
awk -F ':' '{print "USER:"$1," UID:"$3}' /etc/passwd
结果:
USER:root UID:0
USER:bin UID:1
USER:daemon UID:2
USER:adm UID:3
USER:lp UID:4
USER:sync UID:5
USER:shutdown UID:6
USER:halt UID:7
USER:mail UID:8
USER:operator UID:11
USER:games UID:12
USER:ftp UID:14
USER:nobody UID:99
USER:systemd-network UID:192
USER:dbus UID:81
USER:polkitd UID:999
USER:abrt UID:173
USER:tss UID:59
USER:postfix UID:89
USER:chrony UID:998
USER:sshd UID:74
USER:mysql UID:27
案例一
显示/etc/passwd每行的行号,每行的列数,对应行的用户名(print,printf)
awk -F ':' '{print "Line: " NR,"Columns: "NF,"USER: "$1}' /etc/passwd
结果:
Line: 1 Columns: 7 USER: root
Line: 2 Columns: 7 USER: bin
Line: 3 Columns: 7 USER: daemon
Line: 4 Columns: 7 USER: adm
Line: 5 Columns: 7 USER: lp
Line: 6 Columns: 7 USER: sync
Line: 7 Columns: 7 USER: shutdown
Line: 8 Columns: 7 USER: halt
Line: 9 Columns: 7 USER: mail
Line: 10 Columns: 7 USER: operator
Line: 11 Columns: 7 USER: games
Line: 12 Columns: 7 USER: ftp
Line: 13 Columns: 7 USER: nobody
Line: 14 Columns: 7 USER: systemd-network
Line: 15 Columns: 7 USER: dbus
Line: 16 Columns: 7 USER: polkitd
Line: 17 Columns: 7 USER: abrt
Line: 18 Columns: 7 USER: tss
Line: 19 Columns: 7 USER: postfix
Line: 20 Columns: 7 USER: chrony
Line: 21 Columns: 7 USER: sshd
Line: 22 Columns: 7 USER: mysql
awk -F ':' '{printf("Line: %3s Columns: %s USER: %s\n",NR,NF,$1)}' /etc/passwd
结果:
Line: 1 Columns: 7 USER: root
Line: 2 Columns: 7 USER: bin
Line: 3 Columns: 7 USER: daemon
Line: 4 Columns: 7 USER: adm
Line: 5 Columns: 7 USER: lp
Line: 6 Columns: 7 USER: sync
Line: 7 Columns: 7 USER: shutdown
Line: 8 Columns: 7 USER: halt
Line: 9 Columns: 7 USER: mail
Line: 10 Columns: 7 USER: operator
Line: 11 Columns: 7 USER: games
Line: 12 Columns: 7 USER: ftp
Line: 13 Columns: 7 USER: nobody
Line: 14 Columns: 7 USER: systemd-network
Line: 15 Columns: 7 USER: dbus
Line: 16 Columns: 7 USER: polkitd
Line: 17 Columns: 7 USER: abrt
Line: 18 Columns: 7 USER: tss
Line: 19 Columns: 7 USER: postfix
Line: 20 Columns: 7 USER: chrony
Line: 21 Columns: 7 USER: sshd
Line: 22 Columns: 7 USER: mysql
案例二
显示/etc/passwd
中用户组id
大于100
的行号和用户名(if(){}else{})
awk -F ':' '{if ($3>100){ print "Line: "NR,"USER: "$1}else{print "组id小于100,不与展示"}}' passwd
结果:
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
组id小于100,不与展示
Line: 14 USER: systemd-network
组id小于100,不与展示
Line: 16 USER: polkitd
Line: 17 USER: abrt
组id小于100,不与展示
组id小于100,不与展示
Line: 20 USER: chrony
组id小于100,不与展示
组id小于100,不与展示
案例三
显示/etc/passwd中的带有nologin
的行的USER
信息和组id
信息
sed -n '/nologin/p' /etc/passwd | awk -F ':' '{print "USER: "$1,"UID: "$3}'
awk -F ':' '/nologin/{print "USER: "$1,"UID: "$3}' /etc/passwd
结果:
USER: bin UID: 1
USER: daemon UID: 2
USER: adm UID: 3
USER: lp UID: 4
USER: mail UID: 8
USER: operator UID: 11
USER: games UID: 12
USER: ftp UID: 14
USER: nobody UID: 99
USER: systemd-network UID: 192
USER: dbus UID: 81
USER: polkitd UID: 999
USER: abrt UID: 173
USER: tss UID: 59
USER: postfix UID: 89
USER: chrony UID: 998
USER: sshd UID: 74
案例四
统计当前文件夹下的文件/文件夹占用的大小
ls -l | awk 'BEGIN{size=0}{size+=$5}END{print "size is " size "M"}'
结果:
size is 1495034M
案例五
统计/etc/passwd
的账户总人数
awk -F ':' 'BEGIN{count=0}$1!~/^$/{count++}END{print "Line is " count}' passwd
结果:
Line is 22
案例六
统计UID
大于100
的用户名
awk -F ':' 'BEGIN{count=0}{if($3>100) name[count++]=$1}END{for(i=0;i<count;i++){ print i,name[i]}}' passwd
有点蒙b…
案例七
统计netstat -anp
状态下为LISTEN和OCONNECTED的链接数量
netstat -anp | awk '$6~/CONNECTED|LISTEN/{sum[$6]++}END{for (i in sum) {print i,sum[i]}}'
结果:
LISTEN 3
CONNECTED 42
祝你学习愉快~~~