文本:
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
第一种写法:
awk 'gsub(/4842/,4841);{print }' grade.txt
结果如下:有两行的被替代行输出(有一个分号)
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4841 Brown-3 12 26 26
J.Troll 07/99 4841 Brown-3 12 26 26
第二种写法:与第一种的区别在于将{写到了单引号后边。
awk '{gsub(/4842/,4841);print }' grade.txt
结果如下:仅显示出一行的被替代结果,其它全显示。
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4841 Brown-3 12 26 26
第三种写法:与第一种区别是没有了中间的分号,与第二种的区别是没有了分号但{放到了中间。
awk 'gsub(/4842/,4841) {print }' grade.txt
结果如下:
J.Troll 07/99 4841 Brown-3 12 26 26
不知道为什么会出现这种情况,分号在其中的含义是什么?{}的作用到底体现了什么?为什么动了一个{,就会出现第一、二两种不同的结果?
我想man awk是最好的老师了,看其中一段:
| 代码: |
AWK is a line-oriented language. The pattern comes first, and then the action. Action statements are enclosed in { and }. Either the pattern may be missing, or the action may be missing, but, of course, not both. If the pattern is missing, the action is executed for every single record of input. A missing action is equivalent to { print } |