Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created July 31, 2014 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wchargin/b4c8f8eb3aca76662244 to your computer and use it in GitHub Desktop.
Save wchargin/b4c8f8eb3aca76662244 to your computer and use it in GitHub Desktop.
most frequent commands under bash_eternal_history configuration
# histfreq
# history frequency for bash_eternal_history configuration
# see: http://stackoverflow.com/a/19533853/732016
# adapted from http://superuser.com/a/250230/121273
DEFAULT_LINE_COUNT=10
line_count=$1
if [ -z "$1" ]; then
line_count=$DEFAULT_LINE_COUNT
fi
# Get the history.
# This will output lines of the form:
# 1 [1970-01-01 00:00:00] command arg1 arg2
history |
# Remove the timestamp.
# Do this by removing everything between the brackets.
# The lines will now be of the form:
# 1 command arg1 arg2
# which is the standard output of `history,' unmodified.
sed 's/\[[^]]*\]//' |
# Create the frequency list.
# This is implemented in an awk script.
#
# The awk script sets up the following variables:
# * a hash table called CMD, with new elements defaulting to 0
# * the total number of commands in the history file
#
# For each history line, the corresponding command (field 2) is incremented.
# The total count is also incremented.
#
# After all lines are read, the following data is printed for each command:
# <number of invocations> <percentage> <command itself>
awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' |
# Local program invocations are removed, because they are not Linux commands.
grep -v './' |
# The output is formatted into a table with three columns.
column -c3 -s " " -t |
# The percentages are sorted numerically in decreasing order (reversed).
sort -nr |
# Lines are numbered.
nl |
# The output is trimmed to the given number of lines.
head -n $line_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment