Dish
December 27, 2012, 1:38am
1
I have an xml file which has following code :
<abc-ref>
<abc-name>abc.efg.hij.klm</abc-name>
</abc-ref>
I want to comment this whole section out and I have written the following script : (where "hij" is unique string in the file)
TEMPFILE=replaceYY.tmp
file=hello.xml
sed -n '1h;1!H;${;g;s@<abc-ref.*hij*</abc-ref>@<!--Nothing-->@g;p;}' $file > $TEMPFILE
mv $TEMPFILE $file
But it is not working for me! Any help please..
How about something like:
sed '/abc-ref/,/abc-ref/s/^/# /' file
Dish
December 27, 2012, 8:18am
3
sed '/abc-ref/,/abc-ref/s/^/# /' file
This code puts # in front of every line.
Firstly I want to comment only this section out, something like
<!--abc-ref>
<abc-name>abc.efg.hij.klm</abc-name>
</abc-ref-->
Since it is an xml file # will not work here.
Also there are more than one <abc-ref> tags, so I wanted to use "hij" string to make the shell script pattern unique.
---------- Post updated at 06:48 PM ---------- Previous update was at 01:52 PM ----------
Any pointers? This is quite an easy question for experts I guess!
Yoda
December 27, 2012, 12:57pm
4
sed '/abc-ref/,/abc-ref/s/<abc-ref/<!--abc-ref/;s/<\/abc-ref/<\/abc-ref--/' file
Dish
December 27, 2012, 1:10pm
5
But how to make sure the tag <abc-ref> with value
abc.efg.hij.klm is the only one getting commented out?
there are more than one <abc-ref> tags!
RudiC
December 27, 2012, 1:39pm
6
Try this (relying on your sed
's capability to interpret EREs):
$ sed -r '/<abc-ref>/ {N;N;/hij/ {s#(abc-ref)#!--\1#; s#(/abc-ref)#\1--#}}' file
<abc-ref>
<abc-name>abc.efg.saw.klm</abc-name>
</abc-ref>
<!--abc-ref>
<abc-name>abc.efg.hij.klm</abc-name>
</abc-ref-->
<abc-ref>
<abc-name>abc.efg.yxz.klm</abc-name>
</abc-ref>
1 Like
Dish
December 28, 2012, 7:08am
7
rudic:
Try this (relying on your sed
's capability to interpret EREs):
$ sed -r '/<abc-ref>/ {N;N;/hij/ {s#(abc-ref)#!--\1#; s#(/abc-ref)#\1--#}}' file
<abc-ref>
<abc-name>abc.efg.saw.klm</abc-name>
</abc-ref>
<!--abc-ref>
<abc-name>abc.efg.hij.klm</abc-name>
</abc-ref-->
<abc-ref>
<abc-name>abc.efg.yxz.klm</abc-name>
</abc-ref>
And it worked!! :)
Thanks a lot buddy