- 论坛徽章:
- 0
|
目的是想替换begin/end之间的文字:
- [root@m7 tmp]# awk --version
- GNU Awk 3.1.1
- Copyright (C) 1989, 1991-2002 Free Software Foundation.
- [root@m7 tmp]# cat tmpfile
- begin
- replace1
- replace2
- replace3
- end
- continue1
- continue2
- [root@m7 tmp]# cat 1.awk
- BEGIN { update=0; }
- /begin/,/end/ {
- if (!update) {
- print "haha1";
- print "haha2";
- print "haha3";
- update=1;
- }
- next;
- }
- { print $0; }
- [root@m7 tmp]# awk -f 1.awk tmpfile
- haha1
- haha2
- haha3
- continue1
- continue2
- [root@m7 tmp]#
复制代码
以上方法达到我的目的,但是这样:
- [root@m7 tmp]# cat 1.awk
- BEGIN { update=0; }
- /begin/,/end/
- {
- if (!update) {
- print "haha1";
- print "haha2";
- print "haha3";
- update=1;
- }
- next;
- }
- { print $0; }
- [root@m7 tmp]# awk -f 1.awk tmpfile
- begin
- haha1
- haha2
- haha3
- replace1
- replace2
- replace3
- end
- [root@m7 tmp]#
复制代码
仅仅把/begin/,/end/后面的{放到了下一行,结果就不同,为什么? |
|