To Search for a string and to extract the string from the text

Hi Team

I have an huge xml where i need to search for a ceratin numbers. For example

 
2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IFGetResponse>
<model name="BusinessPartner"/>
<rootEntity name="BusinessPartnerStaging"/>
<entities>
<entity name="BusinessPartnerStaging" id="1">
<property value="Siletz Valley Fire District" name="name1"/>
<property value="4000004769" name="businessPartnerId" type="identifier"/>
</entity>
</IFGetResponse>
2014-05-06 15:15:41,499 INFO WebContainer : 10 CommonServicesLogs

From the above xml i need to search for the string 4000004769 and i need to extract the whole xml structure. I know we can extract this by using awk or sed. but how we can search and extract.

i have extracted by using the below awk command

awk '/Entered PUBSUB NOTIFY MESSAGE ()/,/INFO WebContainer/{print}' Services.log.9

But how we include the search part also. Since the line numbers will not be helpful i was stucked. Please help me

With Regards
Kannan nair

So what is the expected output?

Hi Yodha

The prefered output is the xml structure containing that specific value 4000004769

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IFGetResponse>
<model name="BusinessPartner"/>
<rootEntity name="BusinessPartnerStaging"/>
<entities>
<entity name="BusinessPartnerStaging" id="1">
<property value="Siletz Valley Fire District" name="name1"/>
<property value="4000004769" name="businessPartnerId" type="identifier"/>
</entity>
</IFGetResponse>

The xml structure is repeating in the file again and agani with different value for the businesspartnerid.the id value will be unique. so i need to get only that xml structure which has the id value.

With Regards
Kannan nair

Try (untested):

awk '/Entered PUBSUB NOTIFY MESSAGE ()/,/INFO WebContainer/ {TMP=TMP $0 "\n"; if ($0 ~ /4000004769/) L=1} 
      END {if (L) printf "%s", TMP}
    ' Services.log.9

Thanks Rudic

It works. It was an error from my side as i was storing the result of awk to an another variable. But missed the $(awk) part. Thanks for your great help

TMP will collect the entire xml- block as presented. If you have several of those blocks, we need to redesign the short script (which was just an untested sketch anyhow).

1 Like