history-stats 735 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. our %commands;
  5. open(HISTORY, "$ENV{HOME}/.histfile") || die "Cannot open history file: $!\n";
  6. sub hashValueDescendingNum {
  7. $commands{$b} <=> $commands{$a};
  8. }
  9. while (<HISTORY>) {
  10. my $command = $_;
  11. if ($command =~ m/^$/) {
  12. next;
  13. }
  14. my @comm = split(' ', $command);
  15. if ($comm[0] eq "sudo") {
  16. shift @comm;
  17. }
  18. if ($comm[0] eq "..") {
  19. next;
  20. }
  21. $comm[0] =~ s/\.\///;
  22. if (not exists($commands{$comm[0]})) {
  23. $commands{$comm[0]} = 0;
  24. }
  25. $commands{$comm[0]}++;
  26. }
  27. foreach my $key (sort hashValueDescendingNum (keys(%commands))) {
  28. my $value = sprintf "%8s", $commands{$key};
  29. print "$value\t$key\n";
  30. }