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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To Search for a string and to extract the string from the text
# 1  
Old 06-19-2014
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

Code:
 
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


Code:
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

Last edited by Kannannair; 06-19-2014 at 10:26 AM.. Reason: code tags rather than noparse tags
# 2  
Old 06-19-2014
Quote:
Originally Posted by Kannannair
From the above xml i need to search for the string 4000004769 and i need to extract the whole xml structure.
So what is the expected output?
# 3  
Old 06-20-2014
Hi Yodha

The prefered output is the xml structure containing that specific value 4000004769
Code:
<?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

Last edited by Franklin52; 06-20-2014 at 11:08 AM.. Reason: Please use code tags
# 4  
Old 06-20-2014
Try (untested):
Code:
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

# 5  
Old 06-26-2014
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

Last edited by Kannannair; 06-26-2014 at 09:54 AM..
# 6  
Old 06-26-2014
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).
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

help with Scripting - trying to search for string and extract next few characters

Hi I am new to world on unix scripting so any assistance would be gratefully appreciated, I am trying to write a script which reads through a file, reads in line by line, searches for a pattern, copies string after it and then to do a search and replace elsehwere in the line, so the... (7 Replies)
Discussion started by: LonJ_80
7 Replies

7. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

8. Shell Programming and Scripting

Search a string and append text after the string

Hi, I have a file like this... <o t="Batch" id="8410" p="/" g="32"> <a n="name"> <v s="DBBA1MM"/> </a> <a n="owner"> <v r="/Administrator"/> </a> <a n="rights"> <v s="95"/> </a> <a n="debugLevel"> <v s="3"/> </a> <a n="avsStoreLoc"> <v... (8 Replies)
Discussion started by: kesu2k
8 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question