LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-29-2010, 12:02 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
Perl to find regex and print following 5 lines after regex


Alot of time I use a perl one liner like a grep at the end of a cat or more to print specific text from a log or config file. It works great.

more myfile | perl -nle 'print /(regex)/'


what would be awesome is if I could find and print the regex, and the 5 lines following that regex then stop, find the next matching regex and print the next five lines - until end of file.

I think there is a way to do it in Awk and Sed - if there is there should be a way to do it in Perl.
 
Old 08-29-2010, 12:30 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Why not just use grep -A 5 and your regex.
 
Old 08-29-2010, 12:44 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by casperdaghost View Post
Alot of time I use a perl one liner like a grep at the end of a cat or more to print specific text from a log or config file. It works great.

more myfile | perl -nle 'print /(regex)/'


what would be awesome is if I could find and print the regex, and the 5 lines following that regex then stop, find the next matching regex and print the next five lines - until end of file.

I think there is a way to do it in Awk and Sed - if there is there should be a way to do it in Perl.

You may find perlmonks helpful.

In the link above is a snippet from Randal himself
that should give you exactly what you want.


Cheers,
Tink
 
1 members found this post helpful.
Old 08-29-2010, 08:08 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by casperdaghost View Post
Alot of time I use a perl one liner like a grep at the end of a cat or more to print specific text from a log or config file. It works great.

more myfile | perl -nle 'print /(regex)/'
tip for you, there's a tool called a2p you can use to translate awk to Perl code. Eg, awk
Code:
c&&c--;/^Alas/{c=5}
put this as a script, eg myscript.awk, then on command line
Code:
$ a2p test.awk

#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
                        # process any FOO=bar switches

while (<>) {
    print $_ if $c && $c--;;
    if (/^Alas/) {
        $c = 5;
    }
}
what you need is the while(<>) part.
Code:
$ cat test1.pl

#!/usr/bin/perl -w
while (<>) {
    print $_ if $c && $c--;
    if (/^Alas/) {
        $c = 5;
    }
}

$ perl test1.pl file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl regex not matching across multiple lines despite ms flags gfarrell Programming 30 08-18-2010 04:10 AM
how to print 30 lines after regex Poki Linux - Newbie 8 06-11-2009 10:57 AM
grep regex . matches new lines?! lambchops468 Linux - Newbie 3 03-24-2008 09:19 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Searching for 2 empty lines by RegEx in perl mosh Programming 8 09-08-2004 02:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration