in reply to trying to do a parse based on starting position.

Possible solutions:

Command line:

perl -ne 'BEGIN { our $offset=-1 } $offset=2 if /^LUN 40\s*$/; print i +f !$offset--;' 743702.dat

Script:

use strict; my $trigger = -1; while (<>) { $trigger = $. + 2 if /^LUN 40\s*$/; print if $. == $trigger; }
But doesn't work if LUN 40 re-triggers (e.g. two consecutive LUN 40 lines). That might need a list to match against (e.g. using grep).

Replies are listed 'Best First'.
Re^2: trying to do a parse based on starting position.
by ikegami (Patriarch) on Feb 13, 2009 at 22:30 UTC

    But doesn't work if LUN 40 re-triggers

    Since he only wants the last one (tail -1), it's easy to fix.

    my $target = 0; my $match; while (<>) { $target = $. + 2 if /^LUN 40\s*$/; $match = $_ if $. == $target; } print($match) if defined($match);
Re^2: trying to do a parse based on starting position.
by Bloodnok (Vicar) on Feb 14, 2009 at 10:59 UTC
    That's easily fixed ...
    use strict; my $trigger = -1; while (<>) { $trigger = $. + 2 if $trigger != -1 && /^LUN 40\s*$/; # Changed: # print if $. == $trigger; # To: print, last if $. == $trigger && /^LUN 40\s*$/;
    The trigger is armed when and only when the trigger condition hasn't (yet) been hit... so no additional requirement as you suggested in closing :-)

    Update

    Hmmm, having read the question again (and not got as far as ikegami et al's posts which would have told me that), the OP wants only the line in question, not the line c/w all following, hence the solution is easier still...edited accordingly.

    A user level that continues to overstate my experience :-))