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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 01-30-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 24 February 2015, 2:15 PM EST
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts
Using awk

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

Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 01-30-2013
vbe's Unix or Linux Image
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 5 March 2015, 1:28 PM EST
Location: Switzerland - GE
Posts: 6,016
Thanks: 182
Thanked 451 Times in 422 Posts
Are they always situated at the same place (position in the line)?
Sponsored Links
    #5  
Old Unix and Linux 01-30-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 24 February 2015, 2:15 PM EST
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 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 Unix and Linux 01-30-2013
mstafreshi mstafreshi is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 January 2015, 1:50 AM EST
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 Unix and Linux 01-30-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 14 January 2015, 3:24 PM EST
Location: USA
Posts: 1,555
Thanks: 25
Thanked 150 Times in 142 Posts

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 02:44 AM
Extracting particular string in a file and storing matched string in output file sushmab82 Shell Programming and Scripting 14 03-11-2010 03:19 AM
extracting a line based on line number narendra.pant Shell Programming and Scripting 2 09-20-2007 06: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 04:17 AM
extracting from a string preetikate Shell Programming and Scripting 1 03-11-2004 08:08 AM



All times are GMT -4. The time now is 06:29 PM.