Shell 用空格替换双引号
这也许是这里讨论最多的话题之一。我尝试了这里找到的几乎所有的命令和其他调整,但似乎有些地方做得不好 我想用空格/空白替换文件中的所有双引号 当我试图执行这个命令时,我看到了下面的错误Shell 用空格替换双引号,shell,unix,awk,sed,grep,Shell,Unix,Awk,Sed,Grep,这也许是这里讨论最多的话题之一。我尝试了这里找到的几乎所有的命令和其他调整,但似乎有些地方做得不好 我想用空格/空白替换文件中的所有双引号 当我试图执行这个命令时,我看到了下面的错误 sed "s/"/ \''/g' x_orbit.txt > new.tx sed: -e expression #1, char 3: unterminated `s' command 你很接近。只需使用单引号,这样shell就不会尝试在sed命令中展开元字符: sed的/“//g'x_orbit.txt
sed "s/"/ \''/g' x_orbit.txt > new.tx
sed: -e expression #1, char 3: unterminated `s' command
你很接近。只需使用单引号,这样shell就不会尝试在sed命令中展开元字符:
sed的/“//g'x_orbit.txt>new.txt
您可以尝试以下示例:
tr '"' ' ' < x_orbit.txt > new.txt
tr'''new.txt
您提供的脚本:
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中如何工作的根本误解,所以如果我们能帮助纠正这种误解,而不仅仅是纠正脚本,那就好了
在shell中使用任何脚本或字符串时,只需始终将其括在单引号中:
sed 'script' file
var='string'
除非需要使用双引号让变量展开,然后使用双引号,除非需要使用不带引号的引号让全局绑定和文件名展开。一个
awk
版本:
awk '{gsub(/"/," ")}1' file
gsub
用于替换1
总是正确的,所以行是打印的有人能告诉我如何在sed中定义空间吗?一个空间就是一个空间。真正的问题是使用双引号而不是单引号。查看更多详细信息。或者sed'y/“/”x_orbit.txt>new.txt