corboamnesiac
corboamnesiac

Reputation: 1

How to uncomment XML block with SED

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>-->

Upvotes: 0

Views: 692

Answers (1)

Ed Morton
Ed Morton

Reputation: 203807

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.

Upvotes: 1

Related Questions