Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Perl: Reading the next line

Original Message
Name: jb60606
Date: March 24, 2008 at 10:22:26 Pacific
Subject: Perl: Reading the next line
OS: Linux
CPU/Ram: Variable
Model/Manufacturer: 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?


Report Offensive Message For Removal


Response Number 1
Name: jb60606
Date: March 24, 2008 at 10:29:45 Pacific
Subject: Perl: Reading the next line
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: March 24, 2008 at 11:17:30 Pacific
Subject: Perl: Reading the next line
Reply: (edit)
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";
}
}



Report Offensive Follow Up For Removal

Response Number 3
Name: ghostdog
Date: March 24, 2008 at 18:53:01 Pacific
Subject: Perl: Reading the next line
Reply: (edit)
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



Report Offensive Follow Up For Removal

Response Number 4
Name: FishMonger
Date: March 24, 2008 at 19:39:36 Pacific
Subject: Perl: Reading the next line
Reply: (edit)
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-]+)/ );
}



Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Perl: Reading the next line

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12

help mandriva install last straw!

Icon Scaling in Explorer Bar


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC