Computing.Net > Forums > Programming > Perl: Reading the next line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Perl: Reading the next line

Reply to Message Icon

Name: jb60606
Date: March 24, 2008 at 10:22:26 Pacific
OS: Linux
CPU/Ram: Variable
Product: SUN Microsystems
Comment:

I'm writing a script that will search for certain criteria in a file. How do I get it to grab the text immediately following the criteria?

For example, the file contains this text:

Info: Ask 1 | price=135.95 size=100 action=A entries=1 time=09:13:41.247
Info: | id=BATS-ECN size=100 action=A time=10:13:41.240

I want Perl to find the line containing "Ask 1", print the price, then print the ID on the next line.

In the above example, it would print:

price=135.95
id=BATS-ECN

Now, I've got the first part down, doing the following:

@aapl = `cat AAPL_1206022512.txt`;

for (@aapl){
chomp;
@line = split(' ', $_, 9);
if (($line[1] =~ /^Ask$/) && ($line[2] =~ /^1$/))
{
print "$line[1]\n";

};
};

How do you recommend I get it to read the next line containing the "ID".

I have a feeling it's going to be something like counting the number of lines in the file, determining the line number of the line containg "Ask 1", then doing a "$line++" or something to get to the next line. Any recommendations?



Sponsored Link
Ads by Google

Response Number 1
Name: jb60606
Date: March 24, 2008 at 10:29:45 Pacific
Reply:

on a side note, I wish there was a more efficient way of posting text and code on this forum to keep the formatting in line. Computing.net is my favorite computing forum, though it's years behind many others.

I think it's at least ok to increase the width of the forum, to accomodate more text on a single line.

Just my opinion


0

Response Number 2
Name: FishMonger
Date: March 24, 2008 at 11:17:30 Pacific
Reply:

Regarding your side note, I completely agree. There is a lot of good info here, but the site design is outdated and rudimentary.

The best and only way to post formated code is to wrap it in the html pre tags as I will do in the code I post. If all you need is to widen the line width, you can use a continuous string of chars like this.

=========================================================================================

On to Perl.

There are several approaches to your problem, but using cat to load an array would never be considered an acceptable approach.

Useless use of cat:
http://partmaps.org/era/unix/award....

Instead of using cat, I'd use Tie::File
http://search.cpan.org/~mjd/Tie-Fil...


#/usr/bin/perl

use strict;
use warnings;
use Tie::File;

my $filename = 'AAPL_1206022512.txt';
tie my @aapl, 'Tie::File', $filename or die "Can't tie '$filename' $!";

for my $i (0..$#aapl) {
if ( $aapl[$i] =~ /Ask \d+ \| (price=[\d.]+)/ ) {
my $price = $1;
my ($id) = $aapl[$i + 1] =~ /(id=[\w-]+)/;
print "$price\n$id\n";
}
}



0

Response Number 3
Name: ghostdog
Date: March 24, 2008 at 18:53:01 Pacific
Reply:

i guess you are on *nix. If there's no restriction on the language to use, here's an awk solution.

awk 'BEGIN{FS="[=| ]" }
/Ask 1/ {
for(i=1;i<=NF;i++){
if ( $i ~ "price" ) {
print "Price: "$(i+1)
}
}
getline
for(i=1;i<=NF;i++){
if ( $i ~ "id" ) {
print "ID: "$(i+1)
}
}
}' file



0

Response Number 4
Name: FishMonger
Date: March 24, 2008 at 19:39:36 Pacific
Reply:

Here's another Perl approach.


#/usr/bin/perl

use strict;
use warnings;

my $filename = 'AAPL_1206022512.txt';
open my $FILE, '<', $filename or die "can't open $filename $!";

while (<$FILE>) {
print "$1\n" if ( /Ask \d+ \| (price=[\d.]+)/ or /(id=[\w-]+)/ );
}



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Perl: Reading the next line

How to read the 3rd line from the T www.computing.net/answers/programming/how-to-read-the-3rd-line-from-the-t/16604.html

vbscript read line in the middle www.computing.net/answers/programming/vbscript-read-line-in-the-middle/16116.html

VB: TXT lines to ListItem www.computing.net/answers/programming/vb-txt-lines-to-listitem/8812.html