Page 2 of 2

Re: Learning Unix

PostPosted: Mon May 04, 2009 5:38 pm
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.

Re: Learning Unix

PostPosted: Mon Dec 10, 2012 1:39 am
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.

Re: Learning Unix

PostPosted: Mon Dec 10, 2012 7:59 pm
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.