The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
grep patterns - File dhanamurthy Shell Programming and Scripting 1 05-08-2008 10:24 PM
How to get lines in between Patterns? racbern Shell Programming and Scripting 11 04-23-2008 07:28 AM
3 patterns in one line bashuser UNIX for Advanced & Expert Users 1 02-16-2008 07:02 PM
patterns between strings... pbsrinivas Shell Programming and Scripting 6 07-12-2007 01:29 PM
search patterns rochitsharma Shell Programming and Scripting 1 02-27-2006 04:14 AM

Closed Thread
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 04-07-2008
minifish minifish is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 31
get the value between 2 patterns

hello experts,

I want to get the value between 2 patterns.
ex. get hello in <line>hello</line>
Any suggestions?
any sed, grek, awk commands?
  #2 (permalink)  
Old 04-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If the patterns are always on a single line, awk or sed should work fine. If this is a more complex file, look in the Perl FAQ (section 6 IIRC) for some canned solutions.

This is a very frequently recurring question in these forums; try searching some recent threads.

Code:
sed -n 's/.*>\([^<>]*\)<.*/\1/p' file
  #3 (permalink)  
Old 04-07-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Code:
echo "<line>hello</line>" | sed -e 's?\(</.*>\)??' -e 's?\(<.*>\)??'
  #4 (permalink)  
Old 04-07-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
Another approach:

Code:
sed 's/.*>\(.*\)<.*/\1/' file
Regards
  #5 (permalink)  
Old 04-07-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,421
Code:
 echo '<line>hello</line>' | awk -F"\>|\<" '{print $3}'
  #6 (permalink)  
Old 04-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Except that will also get anything outside of the separators. And you don't need the parens if you don't use them for backreferences. Anyway, it would be useful to have more precise requirements. Is there text outside of the separators? Do they span multiple lines? Are there other <tags> which are not to be used as separators? Can there be multiple occurrences of the tag pairs on one line?

Code:
sed -n 's%.*<line>%%; T; s%</line>.*%%p' file
Edit: I was referring to shamrock's solution. Wow, this is a popular thread.
  #7 (permalink)  
Old 04-07-2008
minifish minifish is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 31
Quote:
Originally Posted by era View Post
Except that will also get anything outside of the separators. And you don't need the parens if you don't use them for backreferences. Anyway, it would be useful to have more precise requirements. Is there text outside of the separators? Do they span multiple lines? Are there other <tags> which are not to be used as separators? Can there be multiple occurrences of the tag pairs on one line?

Code:
sed -n 's%.*<line>%%; T; s%</line>.*%%p' file
Edit: I was referring to shamrock's solution. Wow, this is a popular thread.
yes, there are more text outside the separators. I'm trying to change the command people provide me to make it more suitable for my case.
and maybe my sed is too old, I get unrecognized command with
sed -n 's%.*<line>%%; T; s%</line>.*%%p'

Thanks you

file:
<b>asdfasdfasdf</b>
<line>hello</line>
<a>asdfasdfasdf</c>

output:
hello
Closed Thread

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:49 PM.


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