summaryrefslogtreecommitdiffstats
path: root/bin/history-stats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-11-10 14:41:38 -0500
committerJesse Luehrs <doy@tozt.net>2018-11-10 14:43:14 -0500
commit09366721b4a5405ccd24e21c9f0c4edd25d51196 (patch)
treefd72dec2d55f51aecc1b76b01dd7ed18c1be9d87 /bin/history-stats
parent12d2a797346af8fc2908bf5d7873414811a7e9cc (diff)
downloadconf-09366721b4a5405ccd24e21c9f0c4edd25d51196.tar.gz
conf-09366721b4a5405ccd24e21c9f0c4edd25d51196.zip
various script cleanups
Diffstat (limited to 'bin/history-stats')
-rwxr-xr-xbin/history-stats51
1 files changed, 51 insertions, 0 deletions
diff --git a/bin/history-stats b/bin/history-stats
new file mode 100755
index 0000000..b289f97
--- /dev/null
+++ b/bin/history-stats
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Getopt::Long;
+use Path::Class;
+
+my $history_file = $ENV{SHELL} =~ m{/zsh} ? '.zsh_history' : '.bash_history';
+
+my $fh = dir($ENV{HOME})->file($history_file)->openr;
+my $n = 10;
+my $subcommand;
+GetOptions(
+ 'n=i' => \$n,
+ 'subcommand=s' => \$subcommand,
+);
+
+my %cmds;
+my %sudo_cmds;
+
+while (<$fh>) {
+ chomp;
+
+ # strip zsh history timestamps
+ s/^[^;]*;// if /^:/;
+
+ s/^\s*(.*?)\s*$/$1/;
+ my @words = split /\s+/;
+ next unless @words;
+ shift @words while @words && $words[0] =~ /^[A-Z][A-Z0-9_]*=/;
+ next unless @words;
+ my $sudo;
+ if ($words[0] eq 'sudo') {
+ $sudo = 1;
+ shift @words;
+ }
+ if ($subcommand) {
+ next unless $words[0] eq $subcommand;
+ shift @words;
+ shift @words while @words && $words[0] =~ /^-/;
+ }
+ $cmds{($words[0] || '')}++;
+ $sudo_cmds{$words[0]}++ if $sudo;
+}
+
+for my $cmd (sort { $cmds{$b} <=> $cmds{$a} } keys %cmds) {
+ printf '%5d %s', $cmds{$cmd}, $subcommand ? "$subcommand $cmd" : $cmd;
+ printf ' (%d under sudo)', $sudo_cmds{$cmd} if $sudo_cmds{$cmd};
+ print "\n";
+ last unless --$n;
+}