Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing all lines between to key words

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
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.

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
 
The problem is that you have your entire file in one scalar variable, the for loop only executes once, for the entire contents of that variable. You would do better to read the data into an array of lines rather than one scalar, e.g.

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[gray][i]#local $/;      # slurp mode[/i][/gray]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@linesFromFile[/blue]=<DATA>[red];[/red]

[olive][b]for[/b][/olive] [red]([/red] [blue]@linesFromFile[/blue] [red])[/red] [red]{[/red]
    [olive][b]if[/b][/olive] [red]([/red] [red]/[/red][purple]^StartTr1[/purple][red]/[/red] ... [red]/[/red][purple]^oflTr1[/purple][red]/[/red] [red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][red];[/red]
    [red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]StartTr1[/teal]
[teal]sample 1[/teal]
[teal]sample 1[/teal]
...

However, as you can see, the output of that includes the starting and finishing markers, which you don't want. I'm not sure if there is a cleverer way to do it with range expressions, but I'd probably do something like this:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$printit[/blue]=[fuchsia]0[/fuchsia][red];[/red]
[olive][b]for[/b][/olive] [red]([/red] [blue]@linesFromFile[/blue] [red])[/red] [red]{[/red]
    [blue]$printit[/blue]=[fuchsia]0[/fuchsia] [olive][b]if[/b][/olive] [red]/[/red][purple]^oflTr1[/purple][red]/[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [olive][b]if[/b][/olive] [blue]$printit[/blue][red];[/red]
    [blue]$printit[/blue]=[fuchsia]1[/fuchsia] [olive][b]if[/b][/olive] [red]/[/red][purple]^StartTr1[/purple][red]/[/red][red];[/red]
[red]}[/red]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top