1

How can I search for lines beginning with a certain string in an XML file and comment them using perl -pi -e 's/string/replacement/g' /path/file? I know how to use it for basic text replacement but I don't know how to do this.

Specifically, i want to comment out the all shadow lines:

<button function="close" state="prelight" draw_ops="close_focused_prelight"/>
<button function="close" state="pressed" draw_ops="close_focused_pressed"/>
<shadow radius="8.0" opacity="0.75" color="#abde4f" x_offset="1" y_offset="4"/>
<padding left="7" right="7" bottom="7"/>

to

<button function="close" state="prelight" draw_ops="close_focused_prelight"/>
<button function="close" state="pressed" draw_ops="close_focused_pressed"/>
<!--    <shadow radius="8.0" opacity="0.75" color="#abde4f" x_offset="1" y_offset="4"/> -->
<padding left="7" right="7" bottom="7"/>
Veazer
  • 125
  • 5

3 Answers3

0
s/^(<shadow .*)/<!-- $1 -->/mg;
tadmc
  • 3,714
  • 16
  • 14
  • That doesn't seem to work either, sorry I'm very new to perl. i tried perl -pi -e 's/^(/mg;' theme.xml. The modification date changes but the file is the same. – Veazer Jan 31 '12 at 04:04
0
s/(<shadow .*?\/>)/<!-- $1 -->/

The ? indicates a non-greedy match to capture any characters up until '/>', which should match a shadow element even if it is not the only element on a line.

I forgot that you were doing this on the command line. I just validated this with your test input.

perl -pi -e "s/(<shadow .*?\/>)/<\!-- \$1 -->/" /tmp/t
Victor Bruno
  • 1,033
  • 7
  • 12
  • **perl -pi -e 's/^()//g' theme.xml** returned "Unmatched ( in regex" so i escaped the / to **perl -pi -e 's/^()//g' theme.xml** - still no changes – Veazer Jan 31 '12 at 04:18
  • I just saw the link to your file above. Your shadow lines have spaces at the beginning of the line. So, the ^ beginning of line caret needs to be removed. I update my solution. – Victor Bruno Jan 31 '12 at 04:48
  • Excellent, thank you. **perl -pi -e 's/()//g' theme.xml** works perfectly. I just noticed your remark about the command line, does that mean I will need to modify the command for *nix shell scripts? – Veazer Jan 31 '12 at 04:56
  • No it will execute fine in a shell script. I originally assumed this was a within a perl script. On the command line or a shell script the ! and $ both had to be escaped because they have their own meanings to the shell. – Victor Bruno Jan 31 '12 at 04:59
0

Perl oneliner has slight difference in usage among terminals Read this for more information Why doesn't my Perl one-liner work on Windows?

People will assume you are working on linux/unix by default if you dont specify the OS.

For windows use perl -pi.bak -e "s/(^<shadow .*>)/<!-- $1 -->/" theme.xml

For linux perl -pi -e 's/(^<shadow .*>)/<!-- $1 -->/' theme.xml

Community
  • 1
  • 1
jchips12
  • 1,177
  • 1
  • 11
  • 27
  • I'm on linux, but i still can't get this to work. File is here if you wish to test --> https://dl.dropbox.com/s/sralfi7hroio2nx/theme.xml – Veazer Jan 31 '12 at 04:37