Most used commands from history (without perl)

Sample Output
$ mosth
     1  86  8.6%  rpm
     2  64  6.4%  yum
     3  47  4.7%  echo
     4  36  3.6%  ip
     5  36  3.6%  cat
     6  35  3.5%  ls
     7  32  3.2%  cd
     8  27  2.7%  systemctl
     9  27  2.7%  journalctl
    10  25  2.5%  nano

By: nnsense
2015-05-11 17:41:55

What Others Think

> I copied this off the internet OK, so it's fair game for me to tear it to pieces :-) . First, my version of awk (GNU Awk 4.1.1) gives me 6 significant digits rather than the 1 decimal place you have. So, I'll reformat the print: { printf ("%4d %5.1f%% %s\n",CMD[a],CMD[a]/count*100,a); } That's prettier and I don't need the column command any more, . Now we have grep -v "./" Did you copy *that* from the internet? If so bad internet, naughty internet. Was the intention to filter out all commands with a path? grep -v / or just commands that were run from the current directory grep -v "\./" . Let's assume the second (that's what I commonly do) Whatever the case, it would be better to filter out the rubbish before doing the count. But, let awk do it: ... | awk '($2 !~ "^\\./"){ count++ }' . Then comes sort, nl, head. No! Why number hundreds of lines, then only keep 10? It'd be more efficient to: ... | sort -nr | head | nl head gives us 10 lines by default, so no need for any arguments. . So, the new command is: mosth() { history | awk '($2 !~ "^\\./") { CMD[$2]++; count++; } END { for (a in CMD) { printf ("%4d %5.1f%% %s\n",CMD[a],CMD[a]/count*100,a); } }' | sort -nr | head | nl; } Only a few characters shorter, but much more efficient.
flatcap · 284 weeks and 1 day ago
I always learn something, that's why we love linux after all ;)
nnsense · 283 weeks and 6 days ago
This does not work for me.
amoore2600 · 282 weeks and 6 days ago

What do you think?


You must be signed in to comment.


Share Your Commands

Stay in the loop…

Follow the Tweets.



Subscribe to the feeds.


Subscribe to the feed for: