3

I have a xml file having following content.

<contracts>
    <clients>
        <client>
            <name>Nicol</name>
            <clientRef>123</clientRef>
        </client>
        <client>
            <name>Basil</name>
            <clientRef>8234</clientRef>
        </client>
    </clients>
    <entries>
        <entry>
            <regCode>BCG</regCode>
            <clientRef>63352</clientRef>
        </entry>
        <entry>
            <regCode>TYD</regCode>
            <clientRef>3242</clientRef>
        </entry>
    </entries>
</contracts>  

The xml tags 'clientRef' are in both clients and entry sections. However, I need to remove the clientRef tags only in client section.

The desired output is :

<contracts>
    <clients>
        <client>
            <name>Nicol</name>
        </client>
        <client>
            <name>Basil</name>
        </client>
    </clients>
    <entries>
        <entry>
            <regCode>BCG</regCode>
            <clientRef>63352</clientRef>
        </entry>
        <entry>
            <regCode>TYD</regCode>
            <clientRef>3242</clientRef>
        </entry>
    </entries>
</contracts>

I am new to shell and sed commands. How I can remove the clientRef tags with shell scripts?

0
13

Although possible, it is a very, very bad idea to attempt to parse XML or HTML with tools like sed that are based on regular expressions. That can work for simple cases but gets really hard to get right, even for experts, for even slightly more complex cases. So, use an XML parser such as xmlstarlet (should be installable from your operating system's repositories):

$ xmlstarlet ed -d '//client/clientRef' file.xml  
<?xml version="1.0"?>
<contracts>
  <clients>
    <client>
      <name>Nicol</name>
    </client>
    <client>
      <name>Basil</name>
    </client>
  </clients>
  <entries>
    <entry>
      <regCode>BCG</regCode>
      <clientRef>63352</clientRef>
    </entry>
    <entry>
      <regCode>TYD</regCode>
      <clientRef>3242</clientRef>
    </entry>
  </entries>
</contracts>

The ed means "edit this file" and the -d '//client/clientRef' means "remove clientRef entries under client".


In this particular case, you can also use simple text-parsing tools, so I will include an example, but please don't do this for anything more complicated, and be aware that it is likely to break with even a minor change in the input data:

$ awk '{ 
        if(/<clients>/){a=1}
        else if(/<\/clients>/){a=0} 
        if(/<clientRef>/ && a){ next}
       }1;' file.xml 
<contracts>
    <clients>
        <client>
            <name>Nicol</name>
        </client>
        <client>
            <name>Basil</name>
        </client>
    </clients>
    <entries>
        <entry>
            <regCode>BCG</regCode>
            <clientRef>63352</clientRef>
        </entry>
        <entry>
            <regCode>TYD</regCode>
            <clientRef>3242</clientRef>
        </entry>
    </entries>
</contracts>  
1

Using the xq XML parser (part of yq), which is a wrapper around jq (so it's doing XML/JSON trans-coding):

$ xq -x 'del(.contracts.clients.client[].clientRef)' file.xml
<contracts>
  <clients>
    <client>
      <name>Nicol</name>
    </client>
    <client>
      <name>Basil</name>
    </client>
  </clients>
  <entries>
    <entry>
      <regCode>BCG</regCode>
      <clientRef>63352</clientRef>
    </entry>
    <entry>
      <regCode>TYD</regCode>
      <clientRef>3242</clientRef>
    </entry>
  </entries>
</contracts>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.