-1
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <queryResponse xmlns="http://autotask.net/ATWS/v1_6/">
         <queryResult>
            <ReturnCode>1</ReturnCode>
            <EntityResults>
               <Entity xsi:type="Ticket">
                  <id>4359</id>
                  <UserDefinedFields/>
                  <AccountID xsi:type="xsd:int">234</AccountID>
                  <AllocationCodeID xsi:type="xsd:int">79220810309</AllocationCodeID>
                  <CompletedDate xsi:type="xsd:dateTime">2015-10-06T06:42:09.013</CompletedDate>
                  <ContactID xsi:type="xsd:int">3882394</ContactID>
                  <CreateDate xsi:type="xsd:dateTime">2015-10-06T06:34:34.15</CreateDate>
                  <CreatorResourceID xsi:type="xsd:int">4334141</CreatorResourceID>
                  <Description xsi:type="xsd:string">Launch Ec2 Instance</Description>
                  <DueDateTime xsi:type="xsd:dateTime">2015-10-07T22:34:34.15</DueDateTime>
                  <IssueType xsi:type="xsd:int">15</IssueType>
                  <LastActivityDate xsi:type="xsd:dateTime">2016-01-21T07:58:46.913</LastActivityDate>
                  <Priority xsi:type="xsd:int">1</Priority>
                  <QueueID xsi:type="xsd:int">2313</QueueID>
                  <Source xsi:type="xsd:int">-6</Source>
                  <Status xsi:type="xsd:int">4</Status>
                  <SubIssueType xsi:type="xsd:int">445</SubIssueType>
                  <TicketNumber xsi:type="xsd:string">REQ00000007865.0002</TicketNumber>
                  <Title xsi:type="xsd:string">Do something</Title>
                  <FirstResponseDateTime xsi:type="xsd:dateTime">2015-10-06T06:34:34.15</FirstResponseDateTime>
                  <ResolutionPlanDateTime xsi:type="xsd:dateTime">2015-10-06T06:42:00</ResolutionPlanDateTime>
                  <ResolvedDateTime xsi:type="xsd:dateTime">2015-10-06T06:42:00</ResolvedDateTime>
                  <FirstResponseDueDateTime xsi:type="xsd:dateTime">2015-10-06T18:34:34.15</FirstResponseDueDateTime>
                  <ResolvedDueDateTime xsi:type="xsd:dateTime">2015-10-07T22:34:34.15</ResolvedDueDateTime>
                  <ServiceLevelAgreementID xsi:type="xsd:int">880</ServiceLevelAgreementID>
                  <Resolution xsi:type="xsd:string"/>
                  <PurchaseOrderNumber xsi:type="xsd:string"/>
                  <TicketType xsi:type="xsd:int">333</TicketType>
                  <ChangeApprovalType xsi:type="xsd:int">1</ChangeApprovalType>
                  <ChangeApprovalStatus xsi:type="xsd:int">1</ChangeApprovalStatus>
                  <ChangeInfoField1 xsi:type="xsd:string"/>
                  <ChangeInfoField2 xsi:type="xsd:string"/>
                  <ChangeInfoField3 xsi:type="xsd:string"/>
                  <ChangeInfoField4 xsi:type="xsd:string"/>
                  <ChangeInfoField5 xsi:type="xsd:string"/>
                  <LastCustomerNotificationDateTime xsi:type="xsd:dateTime">2015-10-06T06:34:50.927</LastCustomerNotificationDateTime>
                  <LastCustomerVisibleActivityDateTime xsi:type="xsd:dateTime">2015-10-06T06:42:09.013</LastCustomerVisibleActivityDateTime>
                  <TicketCategory xsi:type="xsd:int">3</TicketCategory>
                  <ExternalID xsi:type="xsd:string"/>
                  <FirstResponseInitiatingResourceID xsi:type="xsd:int">296</FirstResponseInitiatingResourceID>
                  <CreatorType xsi:type="xsd:int">1</CreatorType>
                  <CompletedByResourceID xsi:type="xsd:int">11</CompletedByResourceID>
                  <LastActivityPersonType xsi:type="xsd:int">3</LastActivityPersonType>
                  <LastActivityResourceID xsi:type="xsd:int">9</LastActivityResourceID>
               </Entity>
         </queryResult>
      </queryResponse>
   </soap:Body>
</soap:Envelope>

In the above SOAP API response I need to parse the TicketNumber, that is present between <TicketNumber xsi:type="xsd:string">REQ00000007865.0002</TicketNumber>.

Is there a way to achieve this using sed or awk in shell script?

I tried the following but it gave me noithing:

sed -n 's|<TicketNumber xsi:type="xsd:string">\(.*\)</TicketNumber>|\1|p' response.xml

Cyrus
  • 84,225
  • 14
  • 89
  • 153
ANIL
  • 2,542
  • 4
  • 25
  • 44
  • 1
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jun 24 '19 at 13:30
  • Your XML is not valid. There is no closing tag for EntityResults. Is this a copy&paste error? – Cyrus Jun 25 '19 at 03:20

2 Answers2

1

DISCLAIMER: USE DEDICATED XML PARSER IF AVAILABLE

POSIX Solution:

awk -F'[<>]' '/TicketNumber/ {print $3}' file
Community
  • 1
  • 1
vintnes
  • 2,014
  • 7
  • 16
0

Maybe simply like this, with the help of grep and perl:

grep "TicketNumber" response.xml | perl -pe 's/.*>(.*)<.*/\1/'

This idea here is to isolate the TicketNumber tag with grep, and then capture whatever is inside the tag with perl.

Lucas
  • 1,833
  • 1
  • 18
  • 19
  • You can't seriously combine `grep` and `perl`. `perl` alone is enough. Now, if you're using Perl, you might as well use a genuine XML parser. – gniourf_gniourf Jun 24 '19 at 13:37