0

How can i un-comment the block containing the filter tags from my xml file using SED ? Only this block should be un-commented. other commented out code should be left in place.

<a> test <a>
<!--
<filter>
    <filter-name>httpHeaderSecurity</filter-name>
    <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    <async-supported>true</async-supported>
</filter>-->

<!-- <b> leave this one alone </b>-->
  • In your sample input show your target commented block in context surounded by other similar commented and uncommented blocks that you do NOT want changed. That's your sample input. Then add your expected output given that input plus what you've tried so far and at that point you'll have a good question for this forum. Make it clear if you want all commented blocks uncommented or just this one. If the strings `` can appear in other contexts then include those in the example. See [ask] if that's not clear. – Ed Morton Apr 16 '20 at 23:15

1 Answers1

1

With GNU sed for -z, this will uncomment all commented blocks assuming <!-- and --> don't appear in other contexts:

$ sed -z '
    s/@/@A/g; s/{/@B/g; s/}/@C/g; s/<!--/{/g; s/-->/}/g;
    s/{\([^}]*\)}/\1/g;
    s/}/-->/g; s/{/<!--/g; s/@C/}/g; s/{/@B/g; s/@A/@/g
' file
<a> test <a>

<filter>
    <filter-name>httpHeaderSecurity</filter-name>
    <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    <async-supported>true</async-supported>
</filter>

 <b> leave this one alone </b>

If you want to uncomment one specific block then modify s/{\([^}]*\)}/\1/g to whatever regexp works for you, e.g.:

$ sed -z '
    s/@/@A/g; s/{/@B/g; s/}/@C/g; s/<!--/{/g; s/-->/}/g;
    s/{\([^}]*catalina[^}]*\)}/\1/g;
    s/}/-->/g; s/{/<!--/g; s/@C/}/g; s/{/@B/g; s/@A/@/g
' file
<a> test <a>

<filter>
    <filter-name>httpHeaderSecurity</filter-name>
    <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    <async-supported>true</async-supported>
</filter>

<!-- <b> leave this one alone </b>-->

See how to find a search term in source code for what all the substitutions before/after that one are doing.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Thank you. But running that command just prints out the entire XML out on my console and the file is not modified – corboamnesiac Apr 17 '20 at 00:29
  • You cannot and must not parse markup languages with shell or RegEx engines. About XML comments: these are a burden to deal with as there is no straightforward XPath selector for comments, or at least selecting a specific comment node is an issue, as it has no ID, no attribute. Just a raw string. See: [Accessing Comments in XML using XPath](https://stackoverflow.com/q/784745/7939871) – Léa Gris Apr 17 '20 at 00:46
  • @LéaGris if you don't have a more robust alternative to suggest then it's not useful to say "You cannot and must not" do it this way. If you do have a, robust alternative to post then great, let's see it. – Ed Morton Apr 17 '20 at 00:51
  • @corboamnesiac yes, that's what command do by default - print their output to stdout. If you want to modify the original file then redirect the output to a temp file and then move the temp file onto the original `cmd file > tmp && mv tmp file`. Having said that, some seds have a `-i` option to do that for you behind the scenes so check your sed man page, – Ed Morton Apr 17 '20 at 00:53
  • Thank you so much Ed Morton. i have one more question - can i use this same command to insert new tags into the uncommented out xml? within the filter tags afer uncommenting i want to insert test true – corboamnesiac Apr 17 '20 at 01:48