4

I have a lot of text file with specific tag called "Comment", example I have a file called 3dchess.sh,

I need change the tag:

Comment=3D chess for X11 

to

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

The script is:

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

Comment=3D chess for X11
Exec=3Dc

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
  • The problem found for me is: the single quote in Comment=' and </b>' fail also the / between < >, how to solve it? – davidva Feb 27 '14 at 0:03
7

Personally, I like the -r option of sed that allows extended regular expressions so parentheses don't need to be escaped:

sed -r "s#(Comment=)(.*)#\1'<span size=\"xx-large\">\2</b>'#" 3dchess.sh 

Note that since you want to use ' in the substitution, you cannot use sed 's///' but need to use sed "s///" instead. Also, since you need to have / as part of the pattern, you can't use that as a delimiter so you need to choose another character. I picked # but you can use anything you like. As Graeme said, once you're sure this works OK, add the -i switch to make it edit the file in place.

4
sed 's:^Comment=\(.*$\):Comment='\''<span size="xx-large">\1</b>'\': \
  $HOME/3dchess.sh

Add -i when you are sure you have what you want.

Breakdown:

s:x:y: - This is the overall replacement pattern, basically replace x with y. Obviously we have a lot more between the colons above! Usually : is / but sed takes whatever comes after the s and using / here would mean you need to escape it in the pattern.

^Comment= - regex match Comment= at the start of a line ( ^ ). Good practice since it is possible (though unlikely) to get this elsewhere, which would cause problems.

\(.*$\) - match anything ( .* ) up to the end of the line ( $ ). Surrounding with \( \) creates a back reference for use later.

'\'' - close the '' quotes, put a literal ' and reopen quotes. Alternative method to @terdon's.

Comment=<span size="xx-large">\1</b> - replacement string, \1 is the backreference from before.

'\': - close quotes, put a literal ' then the final colon.

  • Hi, thanks; this works only half because doesn't write the single quote Comment=' and the final </b>' :( – davidva Feb 27 '14 at 0:02
  • @davidva, oops missed that. Updated. – Graeme Feb 27 '14 at 9:32
  • thanks, but the output is: 'Comment=3D chess for X11' , then this single quote enclose all tag, don't work – davidva Feb 27 '14 at 21:59
  • @davidva Really? I tested this and it works for me - echo 'Comment=3D chess for X11' | sed 's:^Comment=\(.*$\):'\''Comment=<span size="xx-large">\1</b>'\':. What shell are you using? – Graeme Feb 27 '14 at 22:12
  • xfce4-terminal, but your line helpme with other tag where I needed convert: Icon=0ad to Icon=/usr/share/program/icons/0ad.png. Here: sed 's:^Icon=(.*$):\''Icon=/usr/share/program/icons/\1.png'\: $HOME/0ad.sh – davidva Feb 27 '14 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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