Ancaron
Ancaron

Reputation: 504

Extract lines between two similar strings

I want to extract the lines between one given and one more or less random string. The file looks like :

\\\\\
\\\\\     start 
\\\\\   
lines
to 
extract
\\\\\
\\\\\     <any string> (must not be start but could be sta*) 
\\\\\

Until now I wasn't able to get the lines in between these two without explicitly knowing the second string. By defining the second just as any possible characters using \w or [a-zA-Z] perl seems to match the first line two times and therefore only printing " ----- start " as result.

Until now it looks like this :

open(FILE,'<','file.txt') or die "Could not open: $!";
while(<FILE>){
  print $_ if (/^\\{5}     start$/ .. /^\\{5}     [a-zA-Z]/);
}

and the output is:

\\\\\     start

Hoping for some ideas.

Upvotes: 2

Views: 199

Answers (1)

choroba
choroba

Reputation: 241998

Use ... instead of .. to prevent matching both expressions.

Upvotes: 2

Related Questions