in reply to upto match please, my one-lina!!

I'm adding my $0.02, in case the OP wants (as indicated by the OP's code) to print only the first occurrance of the match.
perl -ne "push @a,$_;m/foo/ and exit 1;shift @a if $#a>3;END{$?==1 and + print @a }" myfile.txt
Update: as japhy points out below, this is Win32 syntax. Use single-quotes for *nix.

     "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

Replies are listed 'Best First'.
Re^2: upto match please, my one-lina!!
by Firefly258 (Beadle) on Jun 09, 2006 at 01:17 UTC
    $ which perl /usr/bin/perl $ perl -ne "push @a,$_; /foo/ and exit 1; shift @a if $#a>3; END{$?==1 + and print @a }" file Bareword found where operator expected at -e line 1, near "0a" (Missing operator before a?) syntax error at -e line 1, near "0a" Execution of -e aborted due to compilation errors.


    That sure is unexplained .. 0a??? where'd that come from. Seems the double quotes are playing a part in inducing this behaviour. Under singles it works fine.
      NetWallah is probably on Win32, where the double-quotes are necessity. Your unixy shell is interpolating $# and replacing it with '0' in your code; thus: shift @a if 0a>3;

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart