-1

i have this xml which i like to comment by string this is my xml ( part of it )

<subsystem
    xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
        <datasource jta="true" jndi-name="java:/comp/env/jdbc/app1" pool-name="app1" enabled="true"
                            use-ccm="false">
            <test1></test1>
            <test2></test2>
        </datasource>
         
            <datasource jta="true" jndi-name="java:/comp/env/jdbc/app2" pool-name="app2" enabled="true"
                            use-ccm="false"><test1></test1><test2></test2></datasource>
             
    </datasources>
</subsystem>

i tried this sed regexp but was able to capture only the first <datasource tag this is what i tried :

sed -E '/./{H;1h;$!d} ; x ; s/(<datasource.*app1*)/ <!--\n\1-->/gi'

based on this section 6.2

i like to capture the and put it in comment so it will look like this

<subsystem
    xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
<!--
        <datasource jta="true" jndi-name="java:/comp/env/jdbc/app1" pool-name="app1" enabled="true"
                            use-ccm="false">
            <test1></test1>
            <test2></test2>
        </datasource>
-->

         
            <datasource jta="true" jndi-name="java:/comp/env/jdbc/app2" pool-name="app2" enabled="true"
                            use-ccm="false"><test1></test1><test2></test2></datasource>
             
    </datasources>
</subsystem>

UPDATE
please dont close the question
i dont have xml tools on my host only basic linux tools

user63898
  • 29,839
  • 85
  • 272
  • 514

2 Answers2

1

i dont have xml tools on my host only basic linux tools

In the absence of XML parsing tools, you can use this basic sed command to comment out a block as you want. Please bear in mind that this will not be a robust solution as offered by tools like xmllint.

You may use this sed:

sed '/<datasource [^>]*app1/,/<\/datasource>/ {s/<datasource /<!--\n\t&/; s~</datasource>~&\n\t-->~;}' file.xml

<subsystem
    xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
    <!--
    <datasource jta="true" jndi-name="java:/comp/env/jdbc/app1" pool-name="app1" enabled="true"
                            use-ccm="false">
            <test1></test1>
            <test2></test2>
        </datasource>
    -->

            <datasource jta="true" jndi-name="java:/comp/env/jdbc/app2" pool-name="app2" enabled="true"
                            use-ccm="false"><test1></test1><test2></test2></datasource>

    </datasources>
</subsystem>

Explanation:

  • /<datasource [^>]*app1/: Match range start from <datasource tag that has text app1 in it somewhere
  • ,/<\/datasource>/: range end with closing </datasource> tag.
  • {: Start action block
    • s/<datasource /<!--\n\t&/;: Prepend opening <datasource tag with a comment start line <!--
    • s~</datasource>~&\n\t-->~;: Append closing </datasource> with comment closing line -->
  • }: End action block
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

With your shown samples in awk please try following.

awk '
/<datasource.*pool-name="app1"/{ found=1 }
found{
  value=(value?value ORS:"")$0
}
/<\/datasource>/ && found{
  print "<!--" ORS value ORS "-->"
  value=found=""
  next
}
1
'  Input_file

Explanation: Adding detailed explanation for above.

awk '                                         ##Starting awk program from here.
/<datasource.*pool-name="app1"/{ found=1 }    ##Checking if line contains <datasource till pool-name="app1" then set found to 1 here.
found{                                        ##Checking if found is set then do following.
  value=(value?value ORS:"")$0                ##Creating value which has current line and keep concatenating its value to it.
}
/<\/datasource>/ && found{                    ##If line contains </datasource> and found is set then do following.
  print "<!--" ORS value ORS "-->"            ##Printing comments section before and after value.
  value=found=""                              ##Nullifying value and found here.
  next                                        ##next will skip all further statements from here.
}
1                                             ##Printing current line here.
'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93