Ubuntu: Use sed to replace a string that contains characters with specials meanings to sed



Question:

I want to search for the string

1 /value Error Code:12345/I  

And replace it with

1 /deleted by user/i  

How can I do this with sed?


Solution:1

sed can use variable separators:

$ echo "1 /value Error Code:12345/I " | sed 's;/value.*/I;/deleted by user/i;'  1 /deleted by user/i  

But if you have to use / for whatever reason, you can always prepend \ to them when they are literal:

$ echo "1 /value Error Code:12345/I " | sed 's/\/value.*\/I/\/deleted by user\/i/;'  1 /deleted by user/i  

Or you could abandon sed altogether and use awk, with / as separator for both input fields and output fields:

$ echo "1 /value Error Code:12345/I " | awk -F'/' 'BEGIN{OFS=FS}{$2="deleted by user";$3="i";print}'  1 /deleted by user/i  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »