3

I have a config file which may OR may not have tasks commented out. If the tasks are commented, I want to "uncomment them"!

sed  -i '/<!--/d; /-->/d testfile

I have this so far but it removes all the comments in the file.

<!--Comment at the top of the file which should not be removed

-->

  <!--
  <task>

  <tag>
  <tag1>keep this content </tag1>
  </tag>

  </task>
-->

I only want to remove comments for task tags which will look like the above in the file (comment won't be on the same line), but keep the content inbetween. Can i use sed to do this without removing any other comments?

Zombo
  • 1
  • 62
  • 391
  • 407
Sarah92
  • 671
  • 4
  • 12
  • 29

1 Answers1

3

This might work for you (GNU sed):

sed '/^\s*<!--/!b;N;/<task>/s/.*\n//;T;:a;n;/^\s*-->/!ba;d' file

This looks for lines beginning with comments. Then reads in the next line and unless that next line contains the <task> tag it leaves them be. If the next line does contain the <task> tag it deletes the first line, then prints following lines until the closing comment tag which it then deletes.

potong
  • 55,640
  • 6
  • 51
  • 83