Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Break one liner command line script after first match

by 2teez (Priest)
on Oct 30, 2013 at 12:51 UTC ( #1060365=note: print w/ replies, xml ) Need Help??


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

How can I modify this so that execution will stop after first match
You can do: perl -ne'/(data)/ && print $1 and last' file.txt

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


Comment on Re: Break one liner command line script after first match
Download Code
Re^2: Break one liner command line script after first match
by McA (Curate) on Oct 30, 2013 at 13:01 UTC

    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060365]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (16)
As of 2014-08-22 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls