Learning Unix

Posts you want to find years later go here.
Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Re: Learning Unix

Post by Jonathan »

Code: Select all

#!/usr/bin/sh
find  -regex '.*/\(SCCS\|RCS\|\.[A-Za-z0-9_]\).*' -prune -or -type l -or -type f | xargs egrep --binary-files=without-match -I "$@" /dev/null
I put this into my path in a file called search and use it all the freaking time. It won't grep object files, it ignores revision control directories, hidden directories, it slices, it dices, it should be a built-in.

Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Re: Learning Unix

Post by Jonathan »

Code: Select all

#!/usr/bin/perl
$optpct = 0;
$optcum = 0;
my $total = 0;
while(<>) {
  next if m/^#/;
  next if m@^/@;
  if( m/^(\d+)\s+(.+)/) {
    $table{"$2\n"} += $1;
    $total += $1;
  } else { # assume single instance
    $table{$_} += 1;
    $total += 1;
  }
}
my $val = 0;
for $key (sort { $table{$b} <=> $table{$a} } keys %table) {
  $pct = sprintf("%f", ( 100*($table{$key} / $total)));
  $tmp = $optpct ? $pct : $table{$key};
  if ($optcum) {
    $val += $tmp;
  } else{
    $val = $tmp;
  }
  print "$val\t$key";
}
Sometimes uniq doesn't give you what you want, which is a frequency count. I present: uniqfreq.

quantus
Tenth Dan Procrastinator
Posts: 4891
Joined: Fri Jul 18, 2003 3:09 am
Location: San Jose, CA

Re: Learning Unix

Post by quantus »

My main comments are:
  • you're not providing option parsing like getOpt or some variant
  • (I would also change the opt variable to be $opt_<var> because I think that's the default scheme for getOpt)
  • you can change the two next lines to one: "next if m@^\s*[#/]@;" and that allows your comments to have leading spaces before them...
  • you really need to start using "my" more consistantly ("use warnings;"?!), or leave it out entirely like in the case of "my $total = 0" or "my $val = 0;" since they'd be treated as 0 how you're using them...
  • I'd add "chomp;" after the "next ..." lines in your while(<>) loop so you don't have to add "\n" to your key in one case, but not the other. Then you can put the "\n" in the print where it makes more sense to apply globally.
Have you clicked today? Check status, then: People, Jobs or Roads

Post Reply