Last Updated: February 25, 2016
·
654
· techlivezheng

Clean ~/.bash_history with timestamp

If you have HISTTIMEFORMAT set in your ~/.bashrc, then your ~/.bash_history file will have a unix timestamp recorded along with each comand, it is not easy to remove duplicate entries from it, below is a one-liner script that could do the work.

cat .bash_history | sed '$!N;s/\n/ /' | sort -s -k 2 | sed -n 'x; /^$/ {x;p;x;d;}; G; /\(#[0-9]*\) \(.*\)\n\(#[0-9]*\) \2$/ !{ s/^[^\n]*\n\(.*\)$/\1/; p; }; d' | sort -s | sed 's/^\(#[0-9]*\) /\1\n/' > bash_history.new