简体   繁体   English

如何用awk中的转义双引号替换双引号?

[英]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.

相关问题 如何解析可以包含转义双引号的双引号分隔字符串 - how to parse a double-quote delimited string that can contain escaped double quotes 如何使用正则表达式将内部双引号替换为单引号 - How to replace inner double quotes to single quote using regex 带有转义双引号的RegEx - RegEx with escaped double quotes 使用正则表达式将 Python 中的转义双引号替换为单引号 - Replace escaped double quotes to single quotes in Python using regex 如何替换csv字段中的双引号,但不向每行添加尾随双引号? - How do I replace double quotes within csv fields but not add a trailing double quote to each line? 用 R 中的双引号替换转义的双引号 - Replacing escaped double quotes by double quotes in R 如何用单引号替换嵌套在另一个双引号中的双引号 - How to replace double quotes nested in another double quotes with single quotes 如何编写一个Scala正则表达式来捕获两个双引号之间的所有引用内容(包括转义的引号)? - How to write a Scala regular expression to capture all the quoted contents between two double quotes(including the escaped quote marks)? RegEx - 如何将两个双引号更改为一个双引号? - RegEx - How to change two double quotes to one double quote? 如何使用 SED 或 AWK 在一行中添加双引号? - How to add double quotes to a line with SED or AWK?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM