in reply to Re: Break one liner command line script after first match
in thread Break one liner command line script after first match

Hi,

as you showed it with your solution: Be aware of the subtle difference between:

perl -ne'/(data)/ && print $1 and last' file.txt perl -ne'/(data)/ && print $1 && last' file.txt

That may bite someone (by the way: I was bitten ;-) )

McA

Replies are listed 'Best First'.
Re^3: Break one liner command line script after first match
by 2teez (Vicar) on Oct 30, 2013 at 13:24 UTC

    Hi,
    Be aware of the subtle difference between:

    perl -ne'/(data)/ && print $1 and last' file.txt perl -ne'/(data)/ && print $1 && last' file.txt
    Of course, I saw the subtle difference, and how you used
    .. print ($1) ...

    the parentheses which is effectively the same thing if "and" is used instead of "&&"
    check this:

    Using
    perl -MO=Deparse -ne'/(data)/ && print $1 && last' file.txt
    you gets:
    LINE: while (defined($_ = <ARGV>)) { print $1 && last if /(data)/; }
    but using
    perl -MO=Deparse -ne'/(data)/ && print ($1) && last' file.txt
    gives:
    LINE: while (defined($_ = <ARGV>)) { last if /(data)/ and print $1; }
    Which is same as this:
    perl -MO=Deparse -ne'/(data)/ && print $1 and last' file.txt
    LINE: while (defined($_ = <ARGV>)) { last if /(data)/ and print $1; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      I was sure that you know the difference. ;)

      I just wanted to point it out for the interested readers. And in the result we get just much more interesting stuff from you. ++ for that.

      Thank you.

      Best regards
      McA