Learning Unix

Posts you want to find years later go here.

Re: Learning Unix

Postby Jonathan » Mon May 04, 2009 5:38 pm

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: 5955
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Re: Learning Unix

Postby Jonathan » Mon Dec 10, 2012 1:39 am

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.
Jonathan
Grand Pooh-Bah
 
Posts: 5955
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Re: Learning Unix

Postby quantus » Mon Dec 10, 2012 7:59 pm

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
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4618
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Previous

Return to The Vault

Who is online

Users browsing this forum: No registered users and 1 guest