Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Command Grep

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

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; } }

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?)

    --bwana147

      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found