-4

Extract the desired content from the file using "xmlstarlet" or "sed" or "awk" or some similar tool. The process should refer the full name of the particular businessprocesses and then should just extract content in between that particular business process.

File from where the content to be extracted.

 <businessProcesses>
        <fullName>Customer Support</fullName>
        <description>Use for Customer Support, TDG and Port</description>
        <isActive>true</isActive>
        <values>
            <fullName>Action Item</fullName>
            <default>false</default>
        </values>
        <values>
            <fullName>Solved</fullName>
            <default>false</default>
        </values>
    </businessProcesses>
    <businessProcesses>
        <fullName>Customer Support - Port</fullName>
        <description>Telecom team use only</description>
        <isActive>true</isActive>
        <values>
            <fullName>New</fullName>
            <default>true</default>
        </values>
        <values>
            <fullName>Open</fullName>
            <default>false</default>
        </values>
    </businessProcesses>
</CustomObject>    

Required output.

<businessProcesses>
        <fullName>Customer Support - Port</fullName>
        <description>Telecom team use only</description>
        <isActive>true</isActive>
        <values>
            <fullName>New</fullName>
            <default>true</default>
        </values>
        <values>
            <fullName>Open</fullName>
            <default>false</default>
        </values>
    </businessProcesses> 
  • 1
    When you post your homework, you should at least provide enough information to solve the problem and look at basic formatting rules. – Ljm Dullaart Jan 24 at 18:46
  • 2
    What research have you made to solve this yourself? – glenn jackman Jan 24 at 19:09
0

Assuming that the XML is well formed (the example lacks a CustomObject opening tag),

$ xmlstarlet sel -t -c '//businessProcesses[fullName="Customer Support - Port"]' -nl  file.xml
<businessProcesses>
        <fullName>Customer Support - Port</fullName>
        <description>Telecom team use only</description>
        <isActive>true</isActive>
        <values>
            <fullName>New</fullName>
            <default>true</default>
        </values>
        <values>
            <fullName>Open</fullName>
            <default>false</default>
        </values>
    </businessProcesses>

This uses XMLStarlet with an XPATH query that extracts a copy of the businessProcesses node that has a fullName child node with the exact value Customer Support - Port.

The -nl at the end adds a newline to the output.


Don't compare XMLStarlet or any other parser of structured document formats with line-oriented text manipulation tools like sed, or awk.

  • **It is throwing the following error when I tried the above command! ** file.xml:1.1: Start tag expected, '<' not found  <businessProcesses> – manoj k Jan 24 at 19:46
  • @manojk Your XML document is malformed. I was assuming that you showed us the actual document. If it has other data in it that is not XML, then this has to be removed first. – Kusalananda Jan 24 at 19:50
0

You can get the desired output (with an extra space) with

awk 'NR==1 || (NR >= 15 && NR <= 26)'

or

sed -n '1p; 15,26p'

If you want a better answer, ask a better question.

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.