Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.240I 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-ECNNow, 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?

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

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/perluse 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";
}
}

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

Here's another Perl approach.
#/usr/bin/perluse 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-]+)/ );
}

![]() |
![]() |
![]() |

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