2

I would like to change the following string

allow ^120\.123\.178\.254$

into

allow ^124\.130\.23\.235$

with sed.

I tried the following but it does not work.

sed -e 's/allow ^120\.123\.178\.254$/allow ^124\.130\.23\.235$/g' /etc/xxx/file.conf

May you can help me?

  • You might want to use -r for different escaping rules. You will still need to escape the backslashes themselves though. – phk Sep 5 '16 at 18:22
2

Your source has \, $, ^ and . characters in them, which will need additional quoting. Also your target has \ which should be quoted

So the resulting string is a little messy:

sed 's/allow \^120\\\.123\\\.178\\\.254\$/allow ^124\\.130\\.23\\.235$/'

The final g you had is only necessary if this string may appear multiple times per line.

For example:

$ cat x
allow ^120\.123\.178\.254$
$ sed 's/allow \^120\\\.123\\\.178\\\.254\$/allow ^124\\.130\\.23\\.235$/' x 
allow ^124\.130\.23\.235$
1

If perl is an option, both the search pattern and replacement can be quoted

$ cat file.conf
allow ^120\.123\.178\.254$

$ perl -pe 's/\Qallow ^120\.123\.178\.254$/q(allow ^124\.130\.23\.235$)/e' file.conf
allow ^124\.130\.23\.235$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.