Find string between two tags

Given the following file:

(...)blablbla start first string stop blabla start_second string.stop blabla(...)

How can I extract the string between the SECOND occurance of the words "start" and "stop" (should return "_second string.").

GREP seems to return only lines, so I guess the solution lies in SED...?
WebDvlpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sentnerCommented:
Use grep to find the string, and then echo it out to the following sed command to reduce it down to the portion you want:

string=`grep $searchpattern $file`
echo $string | sed -e 's/.*start//; s/stop.*//'

0
sentnerCommented:
Also, a second option is to use perl.  For example:

echo $string | perl -ne '/.*start(.*)stop/; print "$1\n"'
0
ozoCommented:
That gives the the string between the last occurrence  of the words "start" and "stop" on each line
if you want the second on each line
echo $string | perl -ne 'print ((/start(.*)stop/g)[1]'
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ozoCommented:
Sorry, I meant to type
echo $string | perl -ne 'print ((/start(.*?)stop/g)[1]'
0
ozoCommented:
Sorry, I meant to type
echo $string | perl -ne 'print ((/start(.*?)stop/g)[1])'
0
WebDvlpAuthor Commented:
Sorry guys, no PERL.

The first solution someone came up with is:

string=`grep $searchpattern $file`
echo $string | sed -e 's/.*start//; s/stop.*//'

But what needs to be put in $searchpattern?? Also, there are no line breaks that can be assumed...
0
sentnerCommented:
Whatever it is that you're using grep to pull the line out of the file.  You could do:

grep "start" $file | grep "stop" | sed -e 's/.*start//; s/stop.*//'

That would give you the last occurrance of a string between start and stop in each line of a file that had start and stop in it.  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Does that mean we can assume there are no line breaks?
That there are no line breaks in between the first and second occurrences of start stop pairs?
That there are exactly two start stop pairs?
That start and stop only occur as part of a pair?
If not perl, would awk be acceptable?
Would we be allowed to insert line breaks as a step toward a solution?
0
ozoCommented:
awk  'BEGIN{ORS=" "}{gsub(/stop/,"\n")}1' file | grep start | head -2 | tail +2 | sed s/.*start//
0
WebDvlpAuthor Commented:
This did the trick, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.