Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Equivalent of Linux Grep Command

by bichonfrise74 (Vicar)
on Jul 13, 2009 at 19:05 UTC ( #779717=perlquestion: print w/ replies, xml ) Need Help??
bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm trying to do a simple equivalent of
grep -A1 -B1 'hello' file_name
in Perl. This basically prints the previous and the next line when the word 'hello' is found. I've done a super search and came up with the following links:

Grep - print matched line and next N lines
Emulating GNU grep -A and -B switches with perl

Both were created back in 2003 and I was wondering if there is now a more concise solution to this. I mean using the Linux 'grep' command, it is able to do it in one line which strikes me odd that it cannot be done in Perl in a concise manner.

Below is my code but again, I think it is too clunky and too long when compared to the Linux 'grep' command.
#!/usr/bin/perl use strict; my ($prev, $next); while (my $line = <DATA>) { if ( $next ) { print "$line"; $next = undef; } if ( $line =~ /hello/ ) { print "$prev" if defined( $prev ); print "$line"; $next++; } $prev = $line; } __DATA__ test1 test2 hello test3 test4
Thanks in advance.

Comment on Equivalent of Linux Grep Command
Select or Download Code
Re: Equivalent of Linux Grep Command
by Corion (Pope) on Jul 13, 2009 at 19:37 UTC

    There is ack, which lives in App::Ack, which is considered to be even better than grep by some, because it also supports file types, recursive descent through file systems and skips VCS directories.

Re: Equivalent of Linux Grep Command
by thunders (Priest) on Jul 13, 2009 at 19:42 UTC

    That's a reasonable start to an implementation. Any solution I can think of involve scanning the file and maintaining @before and @after buffers as you go.

    Perl isn't really intended to provide one liners for every common unix command, and grep itself is a tool not a programming language, any implementation of grep in C is considerably more than one 80 character line of code.

    There is a program called prep on CPAN, which is a pure perl implementation of grep. The source to that program may provide inspiration.

Re: Equivalent of Linux Grep Command
by JavaFan (Canon) on Jul 13, 2009 at 20:41 UTC
    Both were created back in 2003 and I was wondering if there is now a more concise solution to this.
    Since 2003 there has been only one major release of Perl, and none of the new features would make mimicing grep -A1 -B1 any simpler than it already is.
Re: Equivalent of Linux Grep Command
by CountZero (Bishop) on Jul 13, 2009 at 21:19 UTC
    Perl being the "glue language" par excellence, why don't you just shell out and call he Unix grep? There is no shame in relying on tried and trusted tools.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Equivalent of Linux Grep Command
by Your Mother (Canon) on Jul 13, 2009 at 21:26 UTC

    I give a hearty second to ack. Except for the times I need to do huge recursive grepping where grep's speed superiority matters ack is all I use now.

Re: Equivalent of Linux Grep Command
by ambrus (Abbot) on Jul 13, 2009 at 23:10 UTC

    cgrep: Egrep clone with function name display links to a few grep-like programs written in perl. Sure, mine is unmaintained too, and I have a page long TODO for it, but I still have it installed on my computer and use it occasionally.

Re: Equivalent of Linux Grep Command
by petdance (Parson) on Jul 18, 2009 at 06:45 UTC
    Even if you don't want to use ack from the command line, take a look at how it handles the -A and -B flags. It's not very complex.

    xoxo,
    Andy

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2014-08-22 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls