1

I want to search for S+P and replace it with S&P. Following doesn't seem to be working.

sed 's~"S+P"~"S&P"~' filename1 > filename2
amita
  • 13

1 Answers1

2

& is metacharacter and should be escaped for literal matching. Within sed replacement section & points to the whole matched string (in your example, & represents the whole S+P):

sed 's~S+P~S\&P~g' file
  • Thank you that helps. I am now trying to add this two pattern combinations as pipe delimited in a file and reading it from there to replace. But running into similar issue. – amita Oct 24 '17 at 12:01
  • 1
    while read PatternToReplace do origPattern=echo "$PatternToReplace" | sed 's/|/:/' | awk -F: '{ print $1 }' newPattern=echo $PatternToReplace | sed 's/|/:/' | awk -F: '{ print $2 }' sed 's~'"$origPattern"'~'"$newPattern"'~' /tmp/aspgpcf > /tmp/new_aspgpcf mv /tmp/new_aspgpcf /tmp/aspgpcf done < PatternReplacement – amita Oct 24 '17 at 12:05