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

peg - Perl _expression_ (GNU) grep script

by Clarendon4 (Acolyte)
on Nov 14, 2008 at 18:00 UTC ( #723699=perlnews: print w/ replies, xml ) Need Help??

Hope others find "peg" as useful as it has been to me. Give it a try.

Announce - "peg", yet another (GNU) grep like Perl program.

The latest version is available at:

http://cpan.mirrors.uk2.net/authors/id/A/AD/ADAVIES/

Features:

  • Basic synopsis:  peg [OPTION]... PERLEXPR [FILE]...
  • Uses Perl expressions, NOT regular expression patterns.
  • eg% peg /needle/i haystack eg% peg "!(/^=\w/ .. /^=cut/) and /whatever/" *pm
  • ...but automatically converts simple "string" expressions to "/string/".
  • These are identical:

    eg% peg needle haystack eg% peg /needle/ haystack
  • Supports, or has alternatives to, all the commonly used GNU grep options.
  • eg% peg -inHC1Tm1 FOO words.txt words.txt-420- afloat words.txt:421: afoot words.txt-422- aforementioned
  • If no files are specified, does The Right Thing
  • eg% cat haystack | peg -i needle

    ...searches the input stream, while

    eg% peg -i needle

    ...recursively searches each file beneath the cwd.

  • Is configurable through variables set in a configuration file "peg_ini.pl".
  • Options to restrict files searched based on name and last modification time.
  • eg% peg -p "/\.p[ml]$/i" -M 48h whatever

    ...looks in .pm & .pl files modified in the last 2 days.

    Commonly used -p tests of file extensions can be defined:

    $Peg_p{p} = 'pl:pm:t'; # peg_ini.pl

    ...and then used as:

    eg% peg -p p -M 2d whatever
  • The "-z" option prints 'context'.
  • eg% peg -z "/^sub \w+/" foo *pm

    ...prints the last matched "sub" line for any lines matching "foo" ie. (probably) the subroutine that contains "foo".

    Again, common 'contexts' can be defined:

    $Peg_z{p} = '/^(?:\s*sub\s+\w|=head|__(?:END|DATA)__)/'; # peg_ini +.pl

    ...and then used as "-z p".

    eg% peg chmod Temp.pm -nz p **** (331) sub _gettemp { 523: chmod(0600, $path); 545: chmod(0700, $path); **** (645) sub _force_writable { 647: chmod 0600, $file;
  • Customizable command line options.
  • # peg_ini.pl $Peg_longopt{'ignore-dir'} = sub { my $argv_ref = shift; my $dir_name = shift @$argv_ref or die; unshift @$argv_ref, "-p", qq{ \$File !~ m:(^|/)$dir_name/: }; };

    ...would then enable

    eg% peg foo --ignore-dir CVS

    ...to search for foo, ignoring all files in or beneath a directory called CVS.

  • Windows friendly - does filename globbing.
  • Coloured output. (On Win32, this requires Win32::Console::ANSI.)
  • Fast find...
  • As an optimization of the common case of doing a recursive search, an external program can be configured to feed peg the list of files beneath the current directory. The C program qfind.c which is also available at:

    http://cpan.mirrors.uk2.net/authors/id/A/AD/ADAVIES/

    ...is written specifically for this, and provides a significant speed up on Win32 compared to the default of traversing the file system with Perl's File::Find::find. Additionally peg performs parallel processing to maximize performance.

  • Can read and write encoded data.
  • Lots more options. See the "-help".

Comment on peg - Perl _expression_ (GNU) grep script
Select or Download Code
Re: peg - Perl _expression_ (GNU) grep script
by holli (Monsignor) on Nov 14, 2008 at 19:02 UTC
    How would I make this ignore all files beyound a .cvs or .svn directory? Also: ++


    holli, /regexed monk/
      To skip files beneath a directory, use -p with a suitable regex against the $File variable. eg
      peg -p "$File !~ m#(^|/)(?:\.cvs|\.svn)/#" foobar
      But you'll quickly tire of typing that, so add this to your PEG_OPTIONS in your "peg_ini.pl" file:
      $ENV{'PEG_OPTIONS'} .= ' -p "$File !~ m#(^|/)(?:\.cvs|\.svn)/#" ';
        This can now be done much more simply - just add:
        push @Exclude_dirs, ".svn", ".cvs";
        ... to one of the "peg_ini.pl" files.
Re: peg - Perl _expression_ (GNU) grep script
by ambrus (Abbot) on Nov 14, 2008 at 19:45 UTC

      I've looked at both diotalevi's grep and ack, but not cgrep. Thanks for the link.

Re: peg - Perl _expression_ (GNU) grep script
by rovf (Priest) on Nov 19, 2008 at 09:40 UTC

    When I run peg, I get the message

    peg: failed to load Win32::Console::ANSI
    which is strange because I can't find any reference to thie module in the peg source. Aside from this message, the program seems to work fine. Any idea where this message comes from?

    -- 
    Ronald Fischer <ynnor@mm.st>

      When I run peg, I get the messagec peg: failed to load Win32::Console::ANSI

      which is strange because I can't find any reference to thie module in the peg source. Aside from this message, the program seems to work fine. Any idea where this message comes from?

      This module is needed on Win32 to color the output. It is required at line 500 (not sure how you missed it!). NB. you do not call into it... it works by a form of side affect. Check it's docs (see below). If you're not on Win32, then something has gone screwy with the  $Is_Win32 varable.

      Firstly i would highly reccomend installing this as once you've seen how easy and useful writing coloured output is, you'll likely find uses for it your existing scripts.

      Failing that, add  $ENV{PEG_NO_WIN32_CONSOLE_ANSI} = 1 to your peg_ini.pl file and this will stop you seeing the error message. (Yes - it's a hack :-)

      From the modules POD:

      =head1 DESCRIPTION Windows NT/2000/XP does not support ANSI escape sequences in Win32 Con +sole applications. This module emulates an ANSI console for the script that + uses it and also converts the characters from Windows code page to DOS code + page (the so-called ANSI to OEM conversion). This conversion permits the di +splay of the accented characters in the console like in the Windows- based e +ditor used to type the script.
        This module is needed on Win32 to color the output. It is required at line 500 (not sure how you missed it!).

        Neither do I. I looked at it with the 'less' pager and used the search function of less to look for ANSI. I have no idea why I got no match - now I'm doing the same, I can clearly find it.

        Failing that, add $ENV{PEG_NO_WIN32_CONSOLE_ANSI} = 1 to your peg_ini.pl file and this will stop you seeing the error message.

        Wouldn't it make sense to eval {} the loading of the ANSI module and simply switch off colouring if the module is not available? You do it at one place, but not at the other (which makes me wonder why you require the module twice.

        -- 
        Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-07-29 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (215 votes), past polls