Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
go ahead... be a heretic
 
PerlMonks  

Re: Useful addition to Perl?

by tachyon (Chancellor)
on Mar 05, 2004 at 00:50 UTC ( #334084=note: print w/ replies, xml ) Need Help??


in reply to Useful addition to Perl?

I install a few handy dandy widgets in my path. These include re and its inverted cousin re! so I have less typing! re! has 3 extra ! chars to invert the matches. Here is re.

[root@devel3 log]# cat /usr/bin/re #!/usr/bin/perl die " Usage re [RE] <optional filename/dirname> Full Perl grep on STDIN or filename Recursive if filename is a dir " unless @ARGV >= 1; my $re = qr/$ARGV[0]/; if ( $ARGV[1] ) { if ( -d $ARGV[1] ) { # we have a dir so let's recuse require File::Find; File::Find::find( \&grep_file, $ARGV[1] ); } else { open F, $ARGV[1] or die "Can't read $ARGV[1] $!\n"; do{print if m/$re/} while <F>; close F; } } else { do{print if m/$re/} while <STDIN>; } sub grep_file { return unless -f $_; open F, $_ or die "Can't read $File::Find::name $!\n"; my $matches = ''; do{$matches .= $_ if m/$re/} while <F>; print "$File::Find::name\n$matches" if $matches; close F; } [root@devel3 log]# re pri.. /usr/bin/re do{print if m/$re/} while <F>; do{print if m/$re/} while <STDIN>; print "$File::Find::name\n$matches" if $matches; [root@devel3 log]# cat /usr/bin/re | re [A-Z]+IN Full Perl grep on STDIN or filename do{print if m/$re/} while <STDIN>; [root@devel3 log]# cat /usr/bin/re | re "\b[A-Z]+\b" Usage re [RE] <optional filename/dirname> Full Perl grep on STDIN or filename " unless @ARGV >= 1; my $re = qr/$ARGV[0]/; if ( $ARGV[1] ) { [snip] [root@devel3 log]# re perl /devel/www/modperl /devel/www/modperl/logout.pl #!/usr/bin/perl -w /devel/www/modperl/search.pl #!/usr/bin/perl -w /devel/www/modperl/error.pl #!/usr/bin/perl -w [snip] [root@devel3 log]#

YMMV

cheers

tachyon


Comment on Re: Useful addition to Perl?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-04-16 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (417 votes), past polls