how to find replace value with whitespace using se

2019-09-02 15:23发布

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?

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

echoed as

sed -i -e s/\sffff.com-eeee\s/\s/g test.conf

Not working though.

标签: sed replace
2条回答
The star\"
2楼-- · 2019-09-02 16:05

Let me know if this is what you need

echo 'Some values to remove value-to-remove and more' | sed -e 's/\svalue-to-remove\s/CHANGED/g'

output: Some values to removeCHANGEDand more

查看更多
【Aperson】
3楼-- · 2019-09-02 16:18

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
查看更多
登录 后发表回答