Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: One-liner for Matching a Regex in a Directory of Files

by davido (Archbishop)
on Jan 11, 2004 at 06:00 UTC ( #320431=note: print w/ replies, xml ) Need Help??


in reply to One-liner for Matching a Regex in a Directory of Files

I happen to like the following one liner because its output is the entire line that matches, which provides context.

perl -ne "print qq/$. : $_\n/ if m/\bM[A-Z]{3}\b/" testfile.txt

In that one liner, any line that contains a four letter capitalized word starting with M, the whole line will print, so you can get a good idea of where it appears in the file. Also, the line number within the file is printed for added clarity.

The regex uses the \b "word boundry" assertion at both sides of the M[A-Z]{3} construct, to ensure that four and only four letters will trigger a match.


Dave


Comment on Re: One-liner for Matching a Regex in a Directory of Files
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-11-28 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (191 votes), past polls