Commenting Multiple lines using Shell Script

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
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! :frowning:

sed '/abc-ref/,/abc-ref/s/<abc-ref/<!--abc-ref/;s/<\/abc-ref/<\/abc-ref--/' file

But how to make sure the tag <abc-ref> with value
abc.efg.hij.klm is the only one getting commented out? :confused::confused:
there are more than one <abc-ref> tags!

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

And it worked!! :):slight_smile:
Thanks a lot buddy :slight_smile: