perlnewbie9292
Programmer
Hello Perl experts,
I am with no luck been trying to print all lines between two keywords that I have defined.
Not sure what the heck I am doing wrong.
I have a variable "$linesFromFile" which contains all data from a file. If I print the $linesFromFile I get correct results. Now what I am not getting results for is I am trying to print all lines between the two strings shown below, sometimes the (start and end) strings only appear once, sometimes many times. What I am trying to do is print all lines for every time the two strings are found.
Not sure how to on match and save all lines between to search strings. I tried the save () and print $1 with no luck also.
Thanks for the help in advance.
SAMPLE FILE
RESULTS DESIRED
sample 1
sample 1
sample 1
sample 1
SAMPLE 1
sample 2
sample 2
sample 2
sample 2
sample 2
last line before sample 2
I am with no luck been trying to print all lines between two keywords that I have defined.
Not sure what the heck I am doing wrong.
I have a variable "$linesFromFile" which contains all data from a file. If I print the $linesFromFile I get correct results. Now what I am not getting results for is I am trying to print all lines between the two strings shown below, sometimes the (start and end) strings only appear once, sometimes many times. What I am trying to do is print all lines for every time the two strings are found.
Not sure how to on match and save all lines between to search strings. I tried the save () and print $1 with no luck also.
Thanks for the help in advance.
Code:
for ( $linesFromFile ) {
if ( /^StartTr1/gm .. /^oflTr1/gm ) {
print;
}
}
SAMPLE FILE
Code:
StartTr1
sample 1
sample 1
sample 1
sample 1
SAMPLE 1
oflTr1
nothing
nothing
nothing
StartTr1
sample 2
sample 2
sample 2
sample 2
sample 2
last line before sample 2
oflTr1
junk
junk
junk
RESULTS DESIRED
sample 1
sample 1
sample 1
sample 1
SAMPLE 1
sample 2
sample 2
sample 2
sample 2
sample 2
last line before sample 2