AIX下AWK语言中使用正则表达式
在awk命令中使用正则表达式awk中可以使用正则表达式,元字符如下:\,^,.,[],|,(),*,+,?+:匹配一个或多个字符?:匹配模式出现频率,/XY?Z/匹配XYZ,YZ条件操作符操作符 描述——————————— ———————————————< 小于<= 小于等于== 等于!= 不等于>= 大于等于~ 匹配正则表达式!~ 不匹配正则表达式1.匹配grade.txt文件中,第四列的包含Brown的行:awk '{if($4~/Brown/) print $0}' grade.txtJ.Troll 07/99 4842 Brown-3 12 26 26L.Tansley 05/99 4712 Brown-2 12 30 28只要包含Brown,就打印数据:awk '$0 ~ /Brown/' grade.txtJ.Troll 07/99 4842 Brown-3 12 26 26L.Tansley 05/99 4712 Brown-2 12 30 282.精确匹配打印第三列包含42的行:awk '{if($3~/42/) print $0}' grade.txtJ.Troll 07/99 4842 Brown-3 12 26 26打印第三列包含48的行:awk '$3==48 {print $0}' grade.txtP.Bunny 02/99 48 Yello 12 35 283.不匹配打印第三列不包含48的行:awk '$3!~48 {print $0}' grade.txtL.Tansley 05/99 4712 Brown-2 12 30 28查看所有的行:pg grade.txtM.Tansley 05/99 48311 Green 8 40 44J.Lulu 06/99 48317 green 9 24 26P.Bunny 02/99 48 Yello 12 35 28J.Troll 07/99 4842 Brown-3 12 26 26L.Tansley 05/99 4712 Brown-2 12 30 28打印不包含Brown的行:awk '$0 !~/Brown/' grade.txtM.Tansley 05/99 48311 Green 8 40 44J.Lulu 06/99 48317 green 9 24 26P.Bunny 02/99 48 Yello 12 35 28打印第四个域不包含"Brown-2"的行:awk '$4 != "Brown-2" {print $0}' grade.txtM.Tansley 05/99 48311 Green 8 40 44J.Lulu 06/99 48317 green 9 24 26P.Bunny 02/99 48 Yello 12 35 28J.Troll 07/99 4842 Brown-3 12 26 264.大于awk '{if($6 > $7) print $1}' grade.txtP.BunnyL.Tansley5.小于awk '{if( $6 < $7 ) print $0}' grade.txtM.Tansley 05/99 48311 Green 8 40 44J.Lulu 06/99 48317 green 9 24 266.大小写awk '/[Gg]reen/' grade.txtM.Tansley 05/99 48311 Green 8 40 44J.Lulu 06/99 48317 green 9 24 267.任意字符行首第四个字符为a的行awk '$1 ~/^...a/' grade.txtM.Tansley 05/99 48311 Green 8 40 44L.Tansley 05/99 4712 Brown-2 12 30 288.或关系匹配行中包含Yellow或者Brown:awk '$0 ~/(Yellow|Brown)/' grade.txtJ.Troll 07/99 4842 Brown-3 12 26 26L.Tansley 05/99 4712 Brown-2 12 30 28行中包含G(g)reen:awk '$0 ~/(G|g)reen/' grade.txtM.Tansley 05/99 48311 Green 8 40 44J.Lulu 06/99 48317 green 9 24 269.行首:^10.&& andawk '{if ($1=="P.Bunny" && $4=="Yello") print $0}' grade.txtP.Bunny 02/99 48 Yello 12 35 2811.|| orawk '{if ($4=="Yello" || $4~/Brown/) print $0}' grade.txtP.Bunny 02/99 48 Yello 12 35 28J.Troll 07/99 4842 Brown-3 12 26 26L.Tansley 05/99 4712 Brown-2 12 30 2812.!awk '$0 !~/2/' grade.txtM.Tansley 05/99 48311 Green 8 40 44