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
confused sauravjung UNIX for Dummies Questions & Answers 1 04-07-2008 10:08 AM
confused with cp C|[anti-trust] Shell Programming and Scripting 12 05-26-2005 12:55 AM
confused about tar frustrated1 SUN Solaris 5 06-24-2004 10:50 AM
confused hapiworm UNIX for Dummies Questions & Answers 4 06-17-2002 02:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-11-2008
minifish minifish is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 31
sed very confused

Hello experts,
I have this complicated code that output value in between pattern and add "a string" to the front of the output.
The problems I have many pattern that are the same.
so I want to know how to get the value between 1st pattern, 2nd pattern etc.
Any suggestions?

sed -n '/<pattern>/s/.*>\(.*\)<.*/a string\1/gp' file


file:
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
returns:

string=file
string=file2
string=file3
string=file4

but I want a sed command that gets only the value between the 3rd occurrence of pattern

string=file3

Last edited by minifish; 04-11-2008 at 05:04 PM..
  #2 (permalink)  
Old 04-11-2008
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Please post examples.

Jean-Pierre.
  #3 (permalink)  
Old 04-11-2008
minifish minifish is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 31
Quote:
Originally Posted by aigles View Post
Please post examples.

Jean-Pierre.
ok

so
file:
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
returns:

string=file
string=file2
string=file3
string=file4

but I want a sed command that gets only the value between the 3rd occurrence of pattern

string=file3

Last edited by minifish; 04-11-2008 at 04:34 PM..
  #4 (permalink)  
Old 04-11-2008
minifish minifish is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 31
original sed command:
sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
  #5 (permalink)  
Old 04-11-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
Quote:
Originally Posted by minifish View Post
but I want a sed command that gets only the value between the 3rd occurrence of pattern
string=file3
Code:
sed -n '3,/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
  #6 (permalink)  
Old 04-14-2008
minifish minifish is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 31
Quote:
Originally Posted by danmero View Post
Code:
sed -n '3,/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
hm.. I tried this one before, it output all values up to the 1st pattern.

so
file:
<before>blah<\before>
<before>blah1<\before>
<before>blah2<\before>
<Pattern>file<\Pattern>
..
<Pattern>file2<\Pattern>

outputs:

string=blah
string=blah1
string=blah2
string=file
  #7 (permalink)  
Old 04-14-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3}' file
Regards
Sponsored Links
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 07:27 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