Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Last active February 22, 2017 19:52
Show Gist options
  • Select an option

  • Save jhyland87/41a7cca82506398b121070c5d582f424 to your computer and use it in GitHub Desktop.

Select an option

Save jhyland87/41a7cca82506398b121070c5d582f424 to your computer and use it in GitHub Desktop.
# Look through the history for the executions of any commands specified
#
# Example:
# $ function foo { echo "Printing foo"; }
# $ function bar { echo "Printing bar"; }
# $ function baz { echo "Printing baz"; }
# $ foo
# Printing foo
# $ bar
# Printing bar
# $ foo
# Printing foo
# $ bar
# Printing bar
# $ bar
# Printing bar
# $ cmdhistory foo bar baz
# 739 foo
# 740 bar
# 741 foo
# 742 bar
# 743 bar
#
# SUMMARY
# CMD #
# --------- ----
# foo 2
# bar 3
# baz 0
function cmdhistory {
infrom=$(_inputfrom)
if [[ $infrom == 'stdin' ]]
then
read commands
elif [[ $infrom == 'args' ]]
then
commands="$*"
else
return 1
fi
commands=$(echo "${commands}" | sed 's/ /,/g')
history | awk -v commands="${commands}" cmd_pattern="^[a-zA-Z0-9_./-]+$" 'BEGIN {
widest_cmd = 5
cmd_all = 0
if ( length( commands ) > 0 ){
cmd_count = split( commands, cmd_lst, "," )
cmd_counts[ "" ] = ""
cmd_arr[ "" ] = ""
for ( i=0; i<=cmd_count; i++ ){
thiscmd = cmd_lst[ i ]
cmd_arr[ thiscmd ] = thiscmd
cmd_counts[ thiscmd ] = 0
}
}
else {
cmd_all = 1
}
}
{
if( cmd_all || $2 in cmd_arr ){
if ( $2 ~ cmd_pattern ){
cmd_counts[ $2 ]++
print $0
if ( length( $2 ) > widest_cmd )
widest_cmd = length( $2 )
}
}
}
END {
print "\nSUMMARY"
fmt = "%"( widest_cmd + 3 )"s %s\n"
printf fmt, "CMD", "#"
printf fmt, "---------", "----"
for ( cmd in cmd_counts ) {
if ( length(cmd) == 0 ) continue
printf fmt, cmd, cmd_counts[ cmd ]
}
}'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment