2

我正在尝试在 awk 中编写一个脚本,该脚本(除其他外)用两个连续的双引号替换所有双引号实例。但是由于某种原因,我似乎无法正确地转义我的双引号。

例如:

Mary had a "little" lamb

会成为

Mary had a ""little"" lamb

谢谢!

4

1 回答 1

3

你可以这样做:

echo 'Mary had a "little" lamb' | awk '{ gsub(/"/, "&&") } 1' 

输出:

Mary had a ""little"" lamb

最后1调用 awk 的默认块{ print $0 }

使用 sed 更短:

echo 'Mary had a "little" lamb' | sed 's/"/&&/g'
于 2012-12-13T23:02:21.860 回答