how to find replace value with whitespace using se

2019-09-02 16:10发布

问题:

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.

回答1:

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


回答2:

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



标签: sed replace