davidva
davidva

Reputation: 25

sed string replacement containing special characters

I have an input file -- "3dchess.sh" -- whose content is something like:

#!/bin/sh
Package=3dchess
Popcon=48
Section=universe

Comment=3D chess for X11
Exec=3Dc

I need to replace the "Comment" tags from:

Comment=3D chess for X11 

to:

Comment='<span size="xx-large">3D chess for X11</b>'

I have tried this, but it doesn't work in my case:

sed -i ':Comment=:s:Comment=:Comment='<span size="xx-large">:g;/Comment=/s/$/</b>'/' $HOME/3dchess.sh

Upvotes: 0

Views: 73

Answers (1)

csiu
csiu

Reputation: 3269

Try:

echo "Comment=3D chess for X11" | \
  sed  's|Comment=|&'\''<span size="xx-large">|; s|Comment=.*|&</b>'\''|'

This

Comment=3D chess for X11

will become

Comment='<span size="xx-large">3D chess for X11</b>'


EDIT: To the best of my abilities, this is my sed explanation for:

's|Comment=|&'\''<span size="xx-large">|; s|Comment=.*|&</b>'\''|'

Use sed (s) substitute command to replace an old string (Comments=) to new string (&'\''<span size="xx-large">; which is made up of the old string (&) + single quote '\'' + another string (<span size="xx-large">)).

Then (;) do another sed (s) substitute command to replace an old string (Comment=.*; which is made up of a string (Comments=) + anything after that (.*)) to new string (&</b>'\''; which is made up of the old string (&) + another string (</br>) + single quote ('\''))

Upvotes: 1

Related Questions