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
  • 1
    @don_crissti I am aware of this page but that doesn't seem to answer. – amita Oct 24 '17 at 11:59
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
| improve this answer | |
  • 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

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