Home > database >  Comment multiple xml files with sed
Comment multiple xml files with sed

Time:08-10

I want to comment multiple xml lines with sed command.The lines are from a kubernetes container who use tomcat.There are other lines who starts with Valve. I have this:

<!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log" suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

and I want to comment this part:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

so the final output will be:

    <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
<!--         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
 -->

I tried something like this:

sed -r '/^\s*<Valve className\s*=\s*"org\.apache\.catalina\.valves\.AccessLogValve"\s**$/{h;z;N;s:^\n::;H;/^\s*prefix\s*=\s*"localhost\_\access\_\log"\s*\/>\s*$/{g;s/.*/<!--\n&\n-->/}}' 

but it doesn't work.How can I do this? Thanks

CodePudding user response:

This would comment relevant lines out whether the element is in a single line or multiple ones

sed -r -i.bak '/<Valve className=.*AccessLogValve/ s/.*/<!--&/; s/(prefix="localhost_access_log" suffix="\.txt"|pattern="%h .  %s %b") *\/>/& -->/' server.xml

works on both forms

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 
            prefix="localhost_access_log" suffix=".txt" 
            pattern="%h %l %u %t &quot;%r&quot; %s %b" /> 

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 
                pattern="%h %l %u %t &quot;%r&quot; %s %b"
                prefix="localhost_access_log" suffix=".txt" />

CodePudding user response:

Using sed

$ sed -Ez 's/  <Valve className="org\.apache\.catalina\.valves\.AccessLogValve"[^>]*>/<!-- &\n-->/' input_file
<!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
<!--     <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log" suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
-->
  • Related