红联Linux门户
Linux帮助

awk正则匹配列中指定的字符

发布时间:2014-11-12 10:09:45来源:linux网站作者:linux人

范例一
打印指定列中匹配80开头并以80结束的行
关系符号注释
~ 关系操作符
~ 匹配正则
!~ 不匹配正则

[root@linux ~]# cat a.txt
afjdkj 80
lkdjfkja 8080
dfjj 80
jdsalfj 808080
jasj 80
jg 80
linux 80
80 ajfkj
asf 80
80 linux
[root@xingfujie ~]# awk '{if($2~/^80$/)print}' a.txt
afjdkj 80
dfjj 80
jasj 80
jg 80
linux 80
asf 80


范例二
范例 2.1
grep -v ffff过滤ipv6的地址,显示:80的行,删除A到Z和_对应的行,判断已:作为分隔符,打印第三列等于80的行。
[root@linux ~]# netstat -nat | grep -v ffff | grep ":80" | tr -d "[A-Z][_]"| awk -F : '{if($3==80)print}'
tcp        0      0 10.15.201.29:38393          123.125.106.196:80          
tcp        0      0 10.15.201.29:38383          60.28.236.116:80            
tcp        0      0 10.15.201.29:44949          60.28.236.112:80            
tcp        0      0 10.15.201.29:41445          60.28.236.116:80            
tcp        0      0 10.15.201.29:47630          60.28.236.112:80            
tcp        0      0 10.15.201.29:48624          60.28.236.116:80              
tcp        0      0 10.15.201.29:48129          60.28.236.116:80            
tcp        0      0 10.15.201.29:48837          60.28.236.112:80            
tcp        0      0 10.15.201.29:50506          60.28.236.112:80            
tcp        0      0 10.15.201.29:53923          60.28.236.112:80    


范例 2.2
不加grep ":80"一样可以查出正确结果
[root@linux ~]# netstat -nat | grep -v ffff | tr -d "[A-Z][_]"| awk -F : '{if($3==80)print}'
tcp        0      0 10.15.201.29:60047          118.67.112.70:80            
tcp        0      0 10.15.201.29:49038          123.125.106.196:80          
tcp        0      0 10.15.201.29:37035          123.125.106.196:80          
tcp        0      0 10.15.201.29:35936          118.67.112.70:80            
tcp        0      0 10.15.201.29:35972          60.28.236.112:80            
tcp        0      0 10.15.201.29:35056          60.28.236.116:80            
tcp        0      0 10.15.201.29:48777          60.28.236.112:80            
tcp        0      0 10.15.201.29:47173          60.28.236.116:80            
tcp        0      0 10.15.201.29:53014          60.28.236.116:80          
tcp        0      0 10.15.201.29:51816          60.28.236.112:80            
tcp        0      0 10.15.201.29:53109          60.28.236.112:80          
tcp        0      0 10.15.201.29:52274          60.28.236.112:80            
tcp        0      0 10.15.201.29:49854          60.28.236.116:80          
tcp        0      0 10.15.201.29:50339          60.28.236.112:80            
tcp        0      0 10.15.201.29:60595          60.28.236.116:80