Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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)


# Lists commands on the path that contain the given regular expression
# <regular expression>
my $pat=shift || "";
my %printed;
foreach my $p (split ":",$ENV{PATH}) {
   foreach (glob "$p/*") {
      next unless -X && -f;
      my ($path, $name)= m|(.*)\/([^/]*)|;
      print "$_\n" if $name=~m/$pat/o and !$printed{$_};
Comment on Command Grep
Download Code
Replies are listed 'Best First'.
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?)


      Updated, thanks.
Re: Command Grep
by trizen (Hermit) 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?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2015-11-30 02:47 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (756 votes), past polls