Extracting a string from a line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Extracting a string from a line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-30-2013
brunlea brunlea is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 November 2013, 8:46 AM EST
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Extracting a string from a line

Hi,

I have searched all over the forums for a problem similar to mine. I have found many but i have not been able to get them to work for me unfortunately!

What i am attempting to do is to extract part of a string from a line in a file. This line appears multiple times in the file. Example file:


Code:
<SOAP </m:req><m:body><m:string>AAA1234AA</m:string></m:body></m:req></SOAP>
<SOAP </m:req><m:body><m:string>BBB1234BB</m:string></m:body></m:req></SOAP>
<SOAP </m:req><m:body><m:string>CCC1234CC</m:string></m:body></m:req></SOAP>
<SOAP </m:req><m:body><m:string>DDD1234DD</m:string></m:body></m:req></SOAP>

I have tried using

Code:
sed 's/\([A-Z]{3}[0-9]{4}[A-Z]{2}\).*/\1/' file

I expected this to remove all the information on a line apart from the AAA1234AA string. But it does not remove anything at all. Is there any way i extract the string only?

I'm quite inexperienced in unix scripting as you can probably tell. I'm on HP-UX.

Thanks

---------- Post updated at 11:44 AM ---------- Previous update was at 11:21 AM ----------

I have also tried

Code:
sed -n 's/.*\([A-Z]{3}[0-9]{4}[A-Z]{2}\).*/\1/p' file

but this has not worked either.

but if i try

Code:
sed -n 's/.*\(AAA1234AA\).*/\1/p' file

this brings back the desired result (but only relating to that specific string.

Any ideas?
Sponsored Links
    #2  
Old 01-30-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 28 July 2014, 10:00 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 Posts
Using awk

Code:
awk -F'[<|>]' '{ print $8 }' filename

Sponsored Links
    #3  
Old 01-30-2013
brunlea brunlea is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 November 2013, 8:46 AM EST
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
That does not work as i forgot to mention that there are other lines also in the file in the format

Code:
<m:string>zzz1234ZZ</m:string>

Is there a way that command can be adjusted to get the string bearing in mind the different lines in the file?
    #4  
Old 01-30-2013
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 27 July 2014, 5:28 AM EDT
Location: Switzerland - GE
Posts: 5,609
Thanks: 151
Thanked 387 Times in 364 Posts
Are they always situated at the same place (position in the line)?
Sponsored Links
    #5  
Old 01-30-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 28 July 2014, 10:00 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 Posts
Try this instead:

Code:
awk -F'[<|>]' '{for(i=1;i<=NF;i++) { if($i=="m:string") print $(i+1); }}' filename

Sponsored Links
    #6  
Old 01-30-2013
mstafreshi mstafreshi is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 13 May 2014, 2:28 AM EDT
Location: IRAN/TEHRAN
Posts: 18
Thanks: 3
Thanked 2 Times in 2 Posts

Code:
sed 's/.*<m:string>// 
        s/<\/m:string>.*//'  filename

Sponsored Links
    #7  
Old 01-30-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 23 June 2014, 12:35 PM EDT
Location: USA
Posts: 1,438
Thanks: 19
Thanked 136 Times in 128 Posts

Code:
awk -F "<(/)?m:string>" '{print $2}' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting a string matching a pattern from a line jharish Shell Programming and Scripting 8 05-12-2010 01:44 AM
Extracting particular string in a file and storing matched string in output file sushmab82 Shell Programming and Scripting 14 03-11-2010 02:19 AM
extracting a line based on line number narendra.pant Shell Programming and Scripting 2 09-20-2007 05:00 AM
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 03:17 AM
extracting from a string preetikate Shell Programming and Scripting 1 03-11-2004 07:08 AM



All times are GMT -4. The time now is 12:36 AM.