need to go back three lines after a match

A

Andrew Rich

Howdy,

I have a need to do this :-

1. find a match
2. go back three lines
3. read out lines 1 2 3

eg

apples
bannanas
oranges

match on oranges

back up three lines, output three lines

apples
bannanas
oranges
 
E

Eric Schwartz

Andrew Rich said:
Howdy,

I have a need to do this :-

1. find a match
2. go back three lines
3. read out lines 1 2 3

What have you tried? I'd suggest keeping a three-line buffer that you
print out when a line is matched, but the rest of your code might
suggest a different approach.

-=Eric
 
M

Michael Budash

Andrew Rich said:
Howdy,

I have a need to do this :-

1. find a match
2. go back three lines
3. read out lines 1 2 3

eg

apples
bannanas
oranges

match on oranges

back up three lines, output three lines

apples
bannanas
oranges

how bout this:

while (<DATA>) {
push @queue, $_;
shift @queue if @queue == 4;
if (/oranges/) {
print @queue;
last;
}
}
__DATA__
pears
apples
bananas
oranges
peaches
 
K

ko

Andrew said:
Howdy,

I have a need to do this :-

1. find a match
2. go back three lines
3. read out lines 1 2 3

eg

apples
bannanas
oranges

match on oranges

back up three lines, output three lines

apples
bannanas
oranges

You could read the file into an array and reverse() the array (read the
file backwards). When you find your match, print it and the next two
lines. Since the array (lines in the file) is already reversed, you
don't have to backtrack

HTH - keith
 
E

Eric Schwartz

ko said:
You could read the file into an array and reverse() the array (read
the file backwards). When you find your match, print it and the next
two lines. Since the array (lines in the file) is already reversed,
you don't have to backtrack

But that would print them out backwards. You could save the three
lines in an array, reverse it, and then print it out, I suppose. I'd
rather just keep a two-line buffer. :)

-=Eric
 
A

Andrew Rich

I was thinking of sampling three rows at a time

The reason I am doing this is that the data is like this:-

I need to get the date time info on a match on the ascii line

date time info
hex dump
ascii dump
date time info
hex dump
ascii dump
date time info
hex dump
ascii dump
date time info
hex dump
ascii dump
date time info
hex dump
ascii dump
date time info
hex dump
ascii dump
date time info
hex dump
ascii dump
 
T

Tad McClellan

Andrew Rich said:
I was thinking of sampling three rows at a time


Then read three rows each time:

while ( my $line1 = <> ) {
my $line2 = <>;
my $line3 = <>;

# do stuff with the 3 lines
}
 
K

ko

Eric said:
But that would print them out backwards. You could save the three
lines in an array, reverse it, and then print it out, I suppose. I'd
rather just keep a two-line buffer. :)

-=Eric

I was thinking of something (not that its better/more efficient than
keeping the two-line buffer, just different) like :

my @reversed = reverse <FH>;
my $index = 0;
foreach (@reversed) {
if (/PATTERN/) {
print reverse(@reversed[$index .. ($index + 2)]);
last;
}
++$index;
}

Thanks for pointing out what I should have in my explanation :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top