0

The sed command that I use is:

sed -i 's,oldvalue,newvalue,g' *.txt

When I use the above sed command with each of the below examples (1, 2, and 3), the results contain errors.

Example 1

Old value:

AEP/e2cy//rSq,W8VJu\TMb1vGeeyxcr

New value:

/uK2S9v6A\\NnW,PbRB+WUwn/Uu83,Gym

Example 2

Old value

8000::/1

New value

128.0.0.0/1

Example 3

Old value

10.10.77.96/32, fd00:10:10::c18d/128

New value

2001:db8:abcd::/48
1

1 Answer 1

0

Escape metacharacters

You probably forgot to quote metacharacters. For example
xxx\,xxx not xxx,xxx
xxx\\xxx not xxx\xxx

Example 1

In the sed expressions I have replaced \ with \\ and , with \, to indicate that these are to be interpreted as a literal character and not as a metacharacter with special meanings.

$ echo 'foo AEP/e2cy//rSq,W8VJu\TMb1vGeeyxcr bar' \
>   | sed 's,AEP/e2cy//rSq\,W8VJu\\TMb1vGeeyxcr,/uK2S9v6A\\NnW\,PbRB+WUwn/Uu83\,Gym,'
foo /uK2S9v6A\NnW,PbRB+WUwn/Uu83,Gym bar

You cannot use an unescaped comma in a search string or replacement string if you are using commas to delimit them. But you can use a different delimiter to make life easier

$ echo a b,c d | sed 's!b,c!xxx!'
a xxx d

Example 2

I don't see why you had a problem with this.

$ echo foo 8000::/1 bar
foo 8000::/1 bar

$ echo foo 8000::/1 bar | sed 's,8000::/1,128.0.0.0/1,'
foo 128.0.0.0/1 bar

A well written question should include real examples of actual commands, their output and actual error messages. Use cut & paste.

1
  • Thank you for your detailed explanation. I have also taken note of your advice on how to frame a good question. Commented Feb 9 at 12:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.