当前位置: 技术问答>linux和unix
awk的gsub替换问题
来源: 互联网 发布时间:2017-04-23
本文导语: 文本内容如下my-friends: Hans Jurgen School Unter den Linden 78 D-10117 Berlin Germany #SORTKEY: Jones, Adrian Adrian Jones 371 Montgomery Park Road Henley-on-Thames RG9 4AJ UK #SORTKEY: Brown, Kim Kim Brown 1841 S Main Street Westchester, NY 10502 USA ...
文本内容如下my-friends:
Hans Jurgen School
Unter den Linden 78
D-10117 Berlin
Germany
#SORTKEY: Jones, Adrian
Adrian Jones
371 Montgomery Park Road
Henley-on-Thames RG9 4AJ
UK
#SORTKEY: Brown, Kim
Kim Brown
1841 S Main Street
Westchester, NY 10502
USA
现在用awk执行两次替换
cat my-friends |
awk -v RS="" '{gsub("n","^Z");print}' |
awk -v ORS="nn" '{gsub("^Z","n");print}'
输出结果:
#SORTKEY: School, Hans Jurgen^ZHans Jurgen School^ZUnter den Linden 78^ZD-10117 Berlin^ZGermany
#SORTKEY: Jones, Adrian^ZAdrian Jones^Z371 Montgomery Park Road^ZHenley-on-Thames RG9 4AJ^ZUK
#SORTKEY: Brown, Kim^ZKim Brown^Z1841 S Main Street^ZWestchester, NY 10502^ZUSA
为何^Z没被替换掉啊??????
运行平台:Linux version 2.6.18-164.el5 (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 15:51:54 EDT 2009
Hans Jurgen School
Unter den Linden 78
D-10117 Berlin
Germany
#SORTKEY: Jones, Adrian
Adrian Jones
371 Montgomery Park Road
Henley-on-Thames RG9 4AJ
UK
#SORTKEY: Brown, Kim
Kim Brown
1841 S Main Street
Westchester, NY 10502
USA
现在用awk执行两次替换
cat my-friends |
awk -v RS="" '{gsub("n","^Z");print}' |
awk -v ORS="nn" '{gsub("^Z","n");print}'
输出结果:
#SORTKEY: School, Hans Jurgen^ZHans Jurgen School^ZUnter den Linden 78^ZD-10117 Berlin^ZGermany
#SORTKEY: Jones, Adrian^ZAdrian Jones^Z371 Montgomery Park Road^ZHenley-on-Thames RG9 4AJ^ZUK
#SORTKEY: Brown, Kim^ZKim Brown^Z1841 S Main Street^ZWestchester, NY 10502^ZUSA
为何^Z没被替换掉啊??????
运行平台:Linux version 2.6.18-164.el5 (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 15:51:54 EDT 2009
|
cat my-friends |awk -v RS="" '{gsub("n","^Z");print}' |awk -v ORS="nn" '{gsub("\^Z","n");print}'
我觉得是需要转义吧
|
awk -v RS="" 'gsub("n","^Z")' my-friends | awk -v ORS="nn" 'gsub("\^Z"," ")'