473,748 Members | 7,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6780
In article <3f********@new s.iprimus.com.a u>, 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_line s; $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(m y $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
2325
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 advance :( data = "it is an <atag> example of the kind of </atag> data it must handle and another kind of data".split(" ")
3
2224
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 command doesn't go to the third match, it exits out of the loop. I've read everything I could find in NG's, but I can't figure out where I'm going wrong. Here is my code. Function cmdSearch(ctlSearchText As Control, ctlFoundText As Control,...
1
1424
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 original selected Excel rows. Is there any way the I can create a DataTable from CSV that is just droppped onto the form? I tried parsing each line myself, using some regular expressions I found
3
4411
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 really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing version numbers on anything. The errors come and go with no apparent rhyme or reason. We do not...
0
3958
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 Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
2
2816
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, 2007 reference #: 0415 from: operating account
1
7110
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 someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
1
2933
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 CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements: The One that works .... #!C:\Perl\bin\perl.exe # openresolver.cgi # # OpenResolver - a CGI script for...
2
4098
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: 10212009 12 34 56 25 67 90 End Set ******** Set: 2 Date: 10222009
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.