[英]How to replace double quotes by escaped double quote in awk?
In awk how can I replace all double quotes by escaped double quotes? 在awk中如何用转义双引号替换所有双引号?
The dog is "very" beautiful
would become 会成为
The dog is \"very\" beautiful
I've seen this answer ( Using gsub to replace a double quote with two double quotes? ) and I've tried to adapt it, but I'm not very good with awk (and sed is no option because I work both on Linux and OS X and they have different 'sed' installed) 我已经看到了这个答案( 使用gsub用两个双引号替换双引号? )我试图改编它,但我对awk不是很好(并且sed是没有选择因为我在Linux上工作和OS X,他们安装了不同的'sed')
From the answer you linked: 从您链接的答案:
With gsub : 使用gsub :
echo 'The dog is "very" beautiful' | gawk '{ gsub(/"/,"\\\"") } 1'
Alternative, with sed : 替代方案,使用sed :
echo 'The dog is "very" beautiful' | sed 's/"/\\"/g'
In both cases output is: 在这两种情况下输出是:
The dog is \\"very\\" beautiful
这只狗非常漂亮
you may to use with GNU awk: 你可以使用GNU awk:
echo 'The dog is "very" beautiful' | gawk '{ gsub(/"/,"\\\"") } 1'
you get 你得到
The dog is \"very\" beautiful
explanation: 说明:
for special characters \\
and "
, you must to use escape sequence \\\\
and \\"
对于特殊字符
\\
和"
,你必须使用转义序列\\\\
和\\"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.