Remove duplicates in file (without sorting!) leaving the _last_ of the occurences
3
votes
4
answers
572
views
Following https://www.baeldung.com/linux/history-remove-avoid-duplicates , I
added
clean_history() {
awk '!a[$0]++' $HOME/.bash_history > $HOME/.bash_history.tmp && mv $HOME/.bash_history.tmp $HOME/.bash_history
}
trap clean_history DEBUG # execute after every command
to my ~/.bashrc file in order to ensure that after every command is executed, duplicate entries are removed. This is important for me, because kile (the KDE LaTeX editor) leaves a ton of "clear" and "cd ..." commands in the history, effectively destroying the history within a single session of LaTeX-writing. And I have not found any way to prevent kile to do that.
Anyway... the above line of code effectively removes all subsequent entries of a command and leaves the first entry in the history. However, I want the _last_ entry to remain in the history and all previous entries to be deleted. How can do this?
By the way:
Kile ignores
HISTCONTROL=ignoredups:erasedups
so using this approach seems not to be an option.
I would also be happy if we could prevent Kile to write into the history all together.
Non-duplicates:
- https://unix.stackexchange.com/questions/190919/removing-duplicate-entries-in-text-file is leaving the first entry in the file
- so does https://unix.stackexchange.com/questions/525481/how-to-remove-duplicate-lines-inside-a-text-file-get-the-number-of-the-removed
- https://unix.stackexchange.com/questions/20747/how-to-stop-bash-appending-history uses
HISTCONTROL
Asked by Make42
(739 rep)
Jul 1, 2024, 09:43 AM
Last activity: Jul 19, 2024, 04:14 PM
Last activity: Jul 19, 2024, 04:14 PM