用空格替换双引号

时间:2018-08-06 18:56:56

标签: shell unix awk sed grep

这也许是这里讨论最多的主题之一。我尝试了此处找到的几乎所有命令和其他调整,但似乎效果不佳。

我想将文件中的所有双引号替换为空格/空白

当我尝试执行此命令时,看到以下错误。

sed "s/"/ \''/g' x_orbit.txt > new.tx
sed: -e expression #1, char 3: unterminated `s' command

4 个答案:

答案 0 :(得分:5)

您已经关闭。只需使用单引号,因此外壳程序不会尝试在sed命令中扩展元字符:

sed 's/"/ /g' x_orbit.txt > new.txt

答案 1 :(得分:4)

您可以尝试使用tr例如:

tr '"' ' ' < x_orbit.txt > new.txt

答案 2 :(得分:1)

您提供的脚本:

sed "s/"/ \''/g' x_orbit.txt > new.tx

表示:

sed    # invoke sed to execute the following script:
"      # enclose the script in double quotes rather than single so the shell can
       # interpret it (e.g. to expand variables like $HOME) before sed gets to
       # interpret the result of that expansion
s/     # replace what follows until the next /
"      # exit the double quotes so the shell can now not only expand variables
       # but can now do globbing and file name expansion on wildcards like foo*
/      # end the definition of the regexp you want to replace so it is null since
       # after the shell expansion there was no text for sed to read between
       # this / and the previous one (the 2 regexp delimiters)
\'     # provide a blank then an escaped single quote for the shell to interpret for some reason
'/g'    # enclose the /g in single quotes as all scripts should be quoted by default.

这与正确的语法相去甚远,这令人震惊,这就是为什么我在上面对它进行了剖析以帮助您了解所写内容的原因,从而了解了它为什么不起作用。您是从哪里得到这样写的想法(或换种说法-您认为该脚本中的每个字符是什么意思?),我问是因为这表明对Shell中的引用和转义如何工作存在根本的误解,因此如果我们能帮助纠正这种误解,而不只是纠正脚本,那就太好了。

在外壳中使用任何脚本或字符串时,只需始终将其用单引号引起来即可:

sed 'script' file
var='string'

除非您需要使用双引号使变量扩展,然后使用双引号,除非您需要* 不使用引号使通配符和文件名扩展发生。

答案 3 :(得分:1)

awk版本:

awk '{gsub(/"/," ")}1' file

gsub用于替换
1始终为真,因此会打印行

相关问题