in reply to Re: Search a file with ids from another file
in thread Search a file with ids from another file

I had not...bf on my part.

@id_hits looks like this:

G81432 G81765 H43121

And more like that...

So I want to loop through the log for each one of those, find it and capture the "next" line after it.

Replies are listed 'Best First'.
Re^3: Search a file with ids from another file
by blazar (Canon) on May 07, 2007 at 19:21 UTC
    So I want to loop through the log for each one of those, find it and capture the "next" line after it.

    Are the lines which can match the ids in the same order as the ids? To each id does correspond a unique matching line? How many are the ids? (This can make a difference - since you print them, I suppose they're not too many.) Are they all in this form? (i.e. a capital letter followed by five numbers.) Can the "next" line also match one id, or is this impossible a priori?

      There are probably 50 or 60 ids.

      The next line cannot match the one id. I should have posted a copy of that as well but basically 1 id will create 2 lines in the logfile and it is that second line I am trying to get at.

        There are probably 50 or 60 ids.

        Few enough!

        The next line cannot match the one id. I should have posted a copy of that as well but basically 1 id will create 2 lines in the logfile and it is that second line I am trying to get at.

        How 'bout the other question? Well, taking one assumption, one way to do what you're after may be along the lines of the following:

        #!/usr/bin/perl use strict; use warnings; die "Usage: $0 <id_file> [<files>]\n" unless @ARGV; my %id_hit=map { substr($_, 0, 6) => 1 } do { my $idfile=shift; open my $fh, '<', $idfile or die "Can't open `$idfile': $!\n"; <$idfile>; }; while (<>) { if (/([A-Z]\d{5})/ and $id_hit{$1}) { defined(my $next=<>) or die "No 'next' line in `$ARGV' at line $.\n"; print $next; } } __END__