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

2 Answers2

8

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.

terdon
  • 242,166
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.

Graeme
  • 34,027
  • Hi, thanks; this works only half because doesn't write the single quote Comment=' and the final ' :( – davidva Feb 27 '14 at 00:02
  • @davidva, oops missed that. Updated. – Graeme Feb 27 '14 at 09: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
  • @davidva, xfce-terminal is not a shell - http://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con. Copy and paste the highlighted part above, it should work. – Graeme Feb 27 '14 at 22:26
  • Shell text:bash; Shell graphic:XFCE; Linux Fedora – davidva Feb 27 '14 at 22:36
  • @davidva How about bash --version? This shouldn't affect it though, the example will surely work on any bash version. Are you sure everything has been copied correctly? – Graeme Feb 27 '14 at 22:43
  • GNU bash, versión 4.2.45(1)-release (x86_64-redhat-linux-gnu) – davidva Feb 27 '14 at 22:55
  • Exactly the same as me! You can do sed --version ( mine is sed (GNU sed) 4.2.2), but again this should work with all but really old versions. I can't see any other way this won't work apart from a copy error. – Graeme Feb 27 '14 at 23:02
  • http://i61.tinypic.com/9ljqya.png also sed (GNU sed) 4.2.2 – davidva Feb 27 '14 at 23:02
  • @davidva Ok, I see, so the output isn't 'Comment=3D chess for X11' (I thought the html tag was missing). I have moved the ' quote. Hopefully it works as expected now. – Graeme Feb 27 '14 at 23:06