$ cat netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT tcp 0 0 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 tcp 0 0 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED tcp 0 0 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 tcp 0 0 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 tcp 0 0 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK tcp 0 0 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED tcp 0 0 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2 tcp 0 0 :::22 :::* LISTEN
$ awk '{print $1, $4}' netstat.txt Proto Local-Address tcp 0.0.0.0:3306 tcp 0.0.0.0:80 tcp 127.0.0.1:9000 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp :::22
$ awk'{printf "%-8s %-8s %-8s %-18s %-22s %-15s\n",$1,$2,$3,$4,$5,$6}'netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT tcp 0 0 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 tcp 0 0 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED tcp 0 0 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 tcp 0 0 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 tcp 0 0 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK tcp 0 0 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED tcp 0 0 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2 tcp 0 0 :::22 :::* LISTEN
$ awk '$3==0 && $6=="LISTEN" ' netstat.txt tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN
其中的“==”为比较运算符。其他比较运算符:!=, <, < >=, < p>
我们来看看各种过滤记录的方式:
$ awk' $3>0 {print $0}'netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK
如果我们需要表头的话,我们可以引入内建变量NR:
$ awk '$3==0 && $6=="LISTEN" || NR==1 ' netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN
再加上格式化输出:
$ awk'$3==0 && $6=="LISTEN" || NR==1 {printf "%-20s %-20s %s\n",$4,$5,$6}'netstat.txt Local-Address Foreign-Address State 0.0.0.0:3306 0.0.0.0:* LISTEN 0.0.0.0:80 0.0.0.0:* LISTEN 127.0.0.1:9000 0.0.0.0:* LISTEN :::22 :::* LISTEN
说到了内建变量,我们可以来看看awk的一些内建变量:
$0 当前记录(这个变量中存放着整个行的内容) $1~$n 当前记录的第n个字段,字段间由FS分隔 FS 输入字段分隔符 默认是空格或Tab NF 当前记录中的字段个数,就是有多少列 NR 已经读出的记录数,就是行号,从1开始,如果有多个文件话,这个值也是不断累加中。 FNR 当前记录数,与NR不同的是,这个值会是各个文件自己的行号 RS 输入的记录分隔符, 默认为换行符 OFS 输出字段分隔符, 默认也是空格 ORS 输出的记录分隔符,默认为换行符 FILENAME 当前输入文件的名字
怎么使用呢,比如:我们如果要输出行号:
$ awk'$3==0 && $6=="ESTABLISHED" || NR==1 {printf "%02s %s %-20s %-20s %s\n",NR, FNR, $4,$5,$6}'netstat.txt 01 1 Local-Address Foreign-Address State 07 7 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED 08 8 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED 10 10 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED 14 14 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED 17 17 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED
$ awk 'BEGIN{FS=":"} {print $1,$3,$6}' /etc/passwd root 0 /root bin 1 /bin daemon 2 /sbin adm 3 /var/adm lp 4 /var/spool/lpd sync5 /sbin shutdown6 /sbin halt 7 /sbin
上面的命令也等价于:(-F的意思就是指定分隔符)
$ awk-F: '{print $1,$3,$6}'/etc/passwd
注:如果你要指定多个分隔符,你可以这样来:
awk-F '[;:]'
$ awk '$6 ~ /FIN/ || NR==1 {print NR,$4,$5,$6}' OFS="\t" netstat.txt 1 Local-Address Foreign-Address State 6 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 9 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 13 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 18 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2 $ $ awk '$6 ~ /WAIT/ || NR==1 {print NR,$4,$5,$6}' OFS="\t" netstat.txt 1 Local-Address Foreign-Address State 5 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT 6 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 9 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 11 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT 13 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 15 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT 18 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2
上面的示例匹配FIN状态, 第二个示例匹配WAIT字样的状态。其实 ~ 表示模式开始。/ /中是模式。这就是一个正则表达式的匹配。
其实awk可以像grep一样的去匹配第一行,就像这样:
$ awk '/LISTEN/' netstat.txt tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN
我们可以使用 “/FIN|TIME/” 来匹配 FIN 或者 TIME :
$ awk'$6 ~ /FIN|TIME/ || NR==1 {print NR,$4,$5,$6}'OFS="\t"netstat.txt 1 Local-Address Foreign-Address State 5 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT 6 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 9 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 11 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT 13 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 15 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT 18 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2
再来看看模式取反的例子:
$ awk'$6 !~ /WAIT/ || NR==1 {print NR,$4,$5,$6}'OFS="\t"netstat.txt 1 Local-Address Foreign-Address State 2 0.0.0.0:3306 0.0.0.0:* LISTEN 3 0.0.0.0:80 0.0.0.0:* LISTEN 4 127.0.0.1:9000 0.0.0.0:* LISTEN 7 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED 8 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED 10 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED 12 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED 14 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED 16 coolshell.cn:80 208.115.113.92:50601 LAST_ACK 17 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED 19 :::22 :::* LISTEN