Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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{$_};
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});
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://107380]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-06-18 00:50 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.