saumitra121 has asked for the wisdom of the Perl Monks concerning the following question:
Now I want to check(using a regex) whether a pattern exist in file or not. If yes then print the matched text and exit. In case the pattern occurs more than once, then the one liner should break after first match.this is some data this is other data this is new data
Lets for example say that I want to check for pattern "data" in my file. I am using following one liner.
The problem with this is that it is NOT exiting after first match and prints matched content thrice.perl -ne'/(data)/ && print $1' file.txt
How can I modify this so that execution will stop after first match?
Please note that instead of "data" i can provide any regex. Also I can't use a script. This has to be a command line one liner in perl(no awk or grep). File size can be as big as 2 GB.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Break one liner command line script after first match
by McA (Priest) on Oct 30, 2013 at 12:03 UTC | |
by hdb (Monsignor) on Oct 30, 2013 at 12:33 UTC | |
|
Re: Break one liner command line script after first match
by 2teez (Vicar) on Oct 30, 2013 at 12:51 UTC | |
by McA (Priest) on Oct 30, 2013 at 13:01 UTC | |
by 2teez (Vicar) on Oct 30, 2013 at 13:24 UTC | |
by McA (Priest) on Oct 30, 2013 at 13:32 UTC |