LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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, 01:02 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

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, 01:30 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,654

Rep: Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964Reputation: 1964
Why not just use grep -A 5 and your regex.
 
Old 08-29-2010, 01:44 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
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, 09:08 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:10 AM
how to print 30 lines after regex Poki Linux - Newbie 8 06-11-2009 11:57 AM
grep regex . matches new lines?! lambchops468 Linux - Newbie 3 03-24-2008 10:19 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 06:10 AM
Searching for 2 empty lines by RegEx in perl mosh Programming 8 09-08-2004 03:51 AM


All times are GMT -5. The time now is 02:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration