Advertisement
Guest User

Popular history

a guest
Jul 8th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use File::Basename 'basename';
  6.  
  7. sub get_cmds_shell_history()
  8. {
  9.     # Call bash interactive mode and call history bash built-in
  10.     my $get_cmds =
  11.         qq/$ENV{'SHELL'} -i -c "history -r; history"/
  12.         . q/ | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}'/;
  13.  
  14.     chomp(my @cmds = qx# $get_cmds #);
  15.  
  16.     my @cmds_separated;
  17.  
  18.     for (@cmds)
  19.     {
  20.         m/\||\|\||&&/ ?
  21.             push @cmds_separated,                 # Considers to count
  22.                 map { my ($cmd) = split; {$cmd} } # commands between
  23.                     split m/\||\|\||&&/,          # |, || and &&
  24.             :
  25.             push @cmds_separated, (split / /,$_)[0];
  26.     }
  27.  
  28.     return @cmds_separated ? @cmds_separated : undef;
  29. }
  30.  
  31. my %cmds_grouped;
  32.  
  33. (m|/|) ? $cmds_grouped{ basename($_) }++ : $cmds_grouped{$_}++
  34.     for (get_cmds_shell_history);
  35.  
  36. # Popularity sorted decrease order
  37. # Format output with indented numbers
  38. printf("% 8d %s\n", $cmds_grouped{$_}, $_)
  39.     for sort { $cmds_grouped{$b} <=> $cmds_grouped{$a} }
  40.         keys %cmds_grouped;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement