Multi-line grep

perl -ne 'BEGIN{undef $/}; print "$ARGV\t$.\t$1\n" if m/(first line.*\n.*second line)/mg'
Using perl you can search for patterns spanning several lines, a thing that grep can't do. Append the list of files to above command or pipe a file through it, just as with regular grep. If you add the 's' modifier to the regex, the dot '.' also matches line endings, useful if you don't known how many lines you need are between parts of your pattern. Change '*' to '*?' to make it greedy, that is match only as few characters as possible. See also to do a similar thing with awk. Edit: The undef has to be put in a begin-block, or a match in the first line would not be found.

By: hfs
2010-03-18 15:46:10

What Others Think

rock on perlre
linuxrawkstar · 554 weeks and 2 days ago
When I first start doing work in this field I also had the same problems just like you. Then I do my assignment help australia which is a proper research-based guide for me let me know I will share it with you if you want it.
kendrick38 · 35 weeks and 4 days ago
It is really pleasing for me to see that the UW School of Drama has joined your association. Once click pronoun checker online for more guidance. Presently we would have the option to see all the more dazzling fine art.
anonymous11 · 5 weeks ago
For diminishing pressure and mental strain, rehearsing strict qualities would be acceptable. You can visit for more ideas about wiritng. It is additionally essential for getting good vibes and sentiments.
anonymous11 · 5 weeks ago
Jen Coyne has accomplished something truly extraordinary with her stunning show named "Realness Matters". Click subject verb agreement checker online free for more details about writing. This 30-minute program is engaging for me.
anonymous11 · 5 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: