如何在bash脚本中使用sed找到用空格替换的值 [英] how to find replace value with whitespace using sed in a bash script
本文介绍了如何在bash脚本中使用sed找到用空格替换的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这样的文件中有值 ' value-to-remove '(没有 ' 字符).我想使用 sed 来遍历文件并替换前后的值包括空格.我正在通过 bash 脚本运行它.我该怎么做?
I have values in a file like this ' value-to-remove '(without the ' characters). I want to use sed to run through the file and replace the values including the space before and after. I am running this via a bash script. How can I do this?
我目前使用的 sed 命令替换了值,但留下了两个空格.
The sed command I'm using at the moment replaces the values but leaves behind the two spaces.
sed -i 's/ '$value' / /g' test.conf
<小时>
在脚本中我有
In script I have
sed -i -e 's/\s'$DOMAIN'-'$SITE'\s/\s/g' gitosis.conf
回应为
sed -i -e s/\sffff.com-eeee\s/\s/g test.conf
虽然不起作用.
推荐答案
恕我直言,你的 sed 不知道 '\s',所以使用 [ \t]
,并使用双引号,否则你的变量不会扩大.例如:
IMHO your sed does not know '\s', so use [ \t]
, and use double quotes, otherwise your variables will not expand. e.g.:
sed -i -e "s/[ \t]'$DOMAIN'-'$SITE'[ \t]/ /g" gitosis.conf
这篇关于如何在bash脚本中使用sed找到用空格替换的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文