On Mon, Oct 10, 2011 at 4:56 PM, Chris Stinemetz
<chrisstinem...@gmail.com>wrote:

> Any help is appreciated.
>
> Once I match HEH how can alter the program to print the contents that
> are in the  two lines directly above the match?
>
> For example in this case I would like the print results to be:
>
> **01 REPT:CELL 983 CDM 1, CCU 1, CE 5, HEH    Timestamp: 10/10/11 00:01:18
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> while(my $hehline = <DATA>) {
>        chomp $hehline;
>        if ($hehline =~ /, HEH/) {
>        print "$hehline \n";
>        }
> }
>
>
>
> __DATA__
> 10/10/11 00:01:17 #984611
>
> A 01 REPT:CELL 833 CP FAILURE, UNANSWERED TERMINATION
>     CDMA TRAFFIC CHANNEL CONFIRMATION FAILURE
>     TRAFFIC CHANNEL FAILURE REASON - ACQUIRE MOBILE FAILURE [2]
>     DCS 1 TG 1723 TM 374 SG 0 ANT 2
>     CARRIER 4, CHAN UNAVAIL FS-ECP ID 1, SYS ID 4681
>     DN 3168710330, MIN 3164094259, IMSI UNAVAIL
>     SN ###2ddff3 MEID Xa00000###629cc SCM ba
>     ALW CDMA, ASGN CDMA
>     CDM 1, CCU 2, CE 64, MLG 1/MLG_CDM 1
>       DCS 1/PSU 0/SM 3/BHS 6, ECP ID 1, SYS ID 4681
>
>
>
> 10/10/11 00:01:18 #984614
>
> **01 REPT:CELL 983 CDM 1, CCU 1, CE 5, HEH
>     SUPPRESSED MSGS: 0
>     FT PL SECTOR 3 CARRIER 1 (1.9 GHz PCS)
>      FAILURE: OUT OF RANGE
>         PILOT LEVEL: MEASURED = 28.3 dBm    EXPECTED = 33.8 dBm
>     SECONDARY UNIT: CDM 1, CBR 3
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
> Couple of ways.
You could save the line numbers on the first pass, then read the file again
and print the relevant lines; Remember that $. has the current line number.
my @lines;
while (...) {
    ...
    if (/,\s+HEH/) {
        push @lines, $.;
    }
}

Or you could do the same as above, but use Tie::File instead of reading the
file twice.

Or you could keep saving the previous two lines (this assumes the HEH can't
be on the first two lines. If it can, you'll have to modify the proggy
accordingly):
my ($one, $two) = (scalar <DATA>, scalar <DATA>);
while (my $hehline = <DATA>) {
    ...
    if (/,\s+HEH/) {
        say "[$one]\n[$two]";
    }
    ($one, $two) = ($two, $hehline);
}

Or, looking at your data, you could read "paragraphs" instead of
line-by-line -- Apparently each chunk is separated by three (four?)
newlines, so

{
    local $/ = "\n\n\n";
    while (my $hehline = <DATA>) {
        ... # shenanigans here
    }
}

Reply via email to