Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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;
    }
}
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
Node Status?
node history
Node Type: snippet [id://107380]
help
Chatterbox?
[Corion]: I should tell them about the "dim+lock all monitors to show the immediate alert centered" feature of that software so they can announce the next intranet website feature even better ;)
[Eily]: This sounds like the perfect medium to send "don't let anything distract you"
[LanX]: hippo you asked about translating Asterix ?
[Eily]: Maybe the puns in his dialogues were somehow kept through translation, but I can tell he lost his accent
[LanX]: qui?
[Eily]: Astérix
1nickt gives up on finding a Perl YAML parser that supports hash merging. In 2017.
[LanX]: Oh yes true, he lost his accent! xD

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2017-05-24 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?