|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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/' fileI 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' filebut 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
|
||||
|
||||
|
Using
awk Code:
awk -F'[<|>]' '{ print $8 }' filename |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
Are they always situated at the same place (position in the line)?
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Try this instead: Code:
awk -F'[<|>]' '{for(i=1;i<=NF;i++) { if($i=="m:string") print $(i+1); }}' filename |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Code:
sed 's/.*<m:string>//
s/<\/m:string>.*//' filename |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Code:
awk -F "<(/)?m:string>" '{print $2}' file |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|