AWK简介及使用实例(三)

2014-11-24 17:08:28 · 作者: · 浏览: 1
th pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
查找开关是字母M D L的行
[oracle@bys3 ~]$ cat awktest.log |awk '/^[MDL]/'
MMAN started with pid=9, OS id=22862
DBW0 started with pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
查找指定域中后两位是数字,且数字分别是0-9 和0-2的 ~在这表示从结尾算
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,= :]' '$5 ~/[0-9][0-2]$/{print $5}'
10
11
12
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,= :]' '$5 ~/[3-4]$/{print $5}' --查找结尾字母是3-4的
13
14
##################################################################################################

正则逻辑式逻辑运算符:大于小于不等于 及 和&& 或|| 运算

[oracle@bys3 ~]$ cat awktest.log
MMAN started with pid=9, OS id=22862
DBW0 started with pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
CKPT started with pid=12, OS id=22874
SMON:started with pid=13, OS id=22878
RECO:started with pid=14, OS id=22882
显示$5==10||$9>22880 $5等于10或者$9>22880的行
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,= :]' '$5==10 ||$9>22880 {print $5"\t" $9}'
10 22866
14 22882
显示$5>10&&$9>22880 $5大于10并且$9>22880的行
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,= :]' '$5>10&&$9>22880 {print $5"\t" $9}'
14 22882
显示$5!=10 $5!=11 不等于10并且不等于11的行
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,= :]' '$5!=10&&$5!=11 {print $5"\t" $9}'
9 22862
12 22874
13 22878
14 22882
如果print ($5 > 12 这里如果判断$5 > 12是否为真,真则显示冒号前的值,如果不为真,则显示冒号后值
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,= :]' '{print ($5 > 12 "ok \t"$5: "error\t"$5)}'
error 9
error 10
error 11
error 12
ok 13
ok 14