473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need to go back two lines one a match is found

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

Jul 19 '05 #1
2 6765
In article <3f********@news.iprimus.com.au>, Andrew Rich
<vk****@hotmail.com> wrote:
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


I infer that you are asking how to do this in perl. No?

If your file is not too big, you can read the whole thing into an
array, line by line. Then you can access any line:

__CODE__

#!/opt/perl/bin/perl

use strict;

my $infile = 'fruit.dat';
my @array_of_lines;
open(INPUT, $infile) or die "Unable to open $infile: $!\n";
@array_of_lines = <INPUT>;
close(INPUT);

for( my $i = 2; $i <= $#array_of_lines; $i++ ) {
if( $array_of_lines[$i] =~ /oranges/ ) {
print "match found at line $i:\n";
print $array_of_lines[$i-2];
print $array_of_lines[$i-1];
print $array_of_lines[$i];
}
}

__END__

If your file is too big, then you can read the file line-by-line and
keep the last two lines read in an array:

__CODE__

@array_of_lines = ();
open(INPUT, $infile) or die "Unable to open $infile: $!\n";
$array_of_lines[0] = <INPUT>;
$array_of_lines[1] = <INPUT>;
my $n = 2;
while(defined(my $line=<INPUT>)) {
$n++;
print "$n: $line";
if( $line =~ /oranges/ ) {
print "match found at line $n:\n";
print $array_of_lines[0];
print $array_of_lines[1];
print $line;
}
shift(@array_of_lines);
push(@array_of_lines,$line);
}
close(INPUT);

__END__
Jul 19 '05 #2
Andrew Rich <vk****@hotmail.com> wrote in message news:<3f********@news.iprimus.com.au>...
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


# Untested!

my @buffer;
$#buffer = 2;
while (<>) {
push @buffer => $_;
shift @buffer;
print @buffer if /oranges/;
}

This newsgroup does not exist (see FAQ). Please do not start threads here.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
3
by: Liz Malcolm | last post by:
Hello and TIA for guidance. I am building a reusable search procedure (thanks go to Graham Thorpe for his example that set me on my way). Everything works up until the 2nd match is found, the...
1
by: Flack | last post by:
Hello, When the user of my app selects some rows from an open excel file and drag-n-drops them onto my form, I need to be able to parse those lines and create a datatable that would match the...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
2
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07,...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
1
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.