The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Deleting files that don't contain particular text strings / more than one instance of a string kmkocot Shell Programming and Scripting 3 3 Weeks Ago 01:11 AM
extracting numbers from strings gobi Shell Programming and Scripting 2 05-27-2008 12:44 AM
extracting a set of strings from a text file Deanne Shell Programming and Scripting 2 09-21-2007 12:31 AM
Help with extracting strings from a file cmsdelhi Shell Programming and Scripting 7 01-12-2007 09:49 AM
Extracting strings hugow UNIX for Dummies Questions & Answers 1 06-24-2005 07:09 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
fubaya fubaya is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
Extracting text between two strings, first instance only

There are a lot of ways to extract text from between two strings, but what if those strings occur multiple times and you only want the text from the first two strings? I can't seem to find anything to work here. I'm using sed to process the text after it's extracted, so I prefer a sed answer, but whatever works is fine with me.

It's an xml file, the text is between string tags (hope that doesn't cause any confusion). The text may be 1 or 100 lines long and may also contain whitespace, linebreaks, indentions, etc, which shouldn't matter much, but the location of the tags may seem fairly random in relation to the actual text and not a clean "^tagTEXTtag$". I want everything, whitespace, blank lines, etc, between the first open and close tags.


Code:
         <string>This is
         the text 
         that I want
          
          </string>

          <string>text I don't want</string>

         <string>more text
I don't want</string>

  #2 (permalink)  
Old 4 Weeks Ago
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
See here or here for 2 similar examples using gawk. Use "exit" to cause a 1 instance search.
  #3 (permalink)  
Old 3 Weeks Ago
fubaya fubaya is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
Thanks. It took me a while to figure out where to put the "exit". I'm not sure this is correct, but it works.

Code:
awk 'BEGIN{ RS="</string>"}{gsub(/.*<string>/,"")}1{print $RS;exit}' textfile

  #4 (permalink)  
Old 3 Weeks Ago
steadyonabix steadyonabix is offline
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 185
You can trim it a little: -


Code:
awk 'BEGIN{ RS="</string>"}{gsub(/.*<string>/,"");print;exit}' infile

  #5 (permalink)  
Old 3 Weeks Ago
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 548
Or you can use Perl:


Code:
$
$ cat -n f3
     1  <string>This is
     2  the text
     3  that I want
     4  ...
     5  </string>
     6
     7  <string>text I don't want</string>
     8
     9  <string>more text
    10  I don't want</string>
$
$ perl -ne 'BEGIN{undef $/} /<string>(.*?)<\/string>/s and print $1' f3
This is
the text
that I want
...
$
$

tyler_durden
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0