Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Command Grep

by fundflow (Chaplain)
on Aug 23, 2001 at 20:13 UTC ( #107380=snippet: print w/ replies, xml ) Need Help??

Description: This short snippet searches the current path for commands that contain the argument (that can be a regular expression)

This is a two-minute effort that helped me find out that redhat ships with 'gcolorsel' which replaces the old 'xcolors'.

(Now cleaned and documented for your viewing pleasure)

#!/usr/bin/perl

# Lists commands on the path that contain the given regular expression
+.
#
# USAGE:
# comgrep.pl <regular expression>
my $pat=shift || "";
my %printed;
foreach my $p (split ":",$ENV{PATH}) {
   foreach (glob "$p/*") {
      next unless -X && -f;
      s|//|/|g;
      my ($path, $name)= m|(.*)\/([^/]*)|;
      print "$_\n" if $name=~m/$pat/o and !$printed{$_};
      $printed{$_}=1;
    }
}
Comment on Command Grep
Download Code
Re: Command Grep
by bwana147 (Pilgrim) on Aug 29, 2001 at 15:01 UTC

    Nice! Two comments though:

    • Since $pat doesn't ever change, you can use the /o modifier to prevent the recompilation of the regex at each loop: $name =~ /$pat/o
    • Since you use pipes as pattern delimiter, the slash looses its special meaning and no longer needs be escaped: s|//|/|g;. That's easier to read, IMHO (btw, what's the use of this?)

    --bwana147

      Updated, thanks.
Re: Command Grep
by trizen (Friar) on Dec 08, 2011 at 00:55 UTC
    Same thing, but in fewer characters...
    my $r = shift() // ''; -X -f && substr($_,1+rindex$_,'/')=~/$r/o && print"$_\n"for(map{glob"$_/*"}split/:/,$ENV{PATH});

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-09-18 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (115 votes), past polls