Last active
October 9, 2024 22:26
-
-
Save calexandre/63547c8dd0e08bf693d298c503e20aab to your computer and use it in GitHub Desktop.
Merge two zsh history files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Inspired on https://david-kerwick.github.io/2017-01-04-combining-zsh-history-files/ | |
set -e | |
history1=$1 | |
history2=$2 | |
merged=$3 | |
echo "Merging history files: $history1 + $history2" | |
test ! -f $history1 && echo "File $history1 not found" && exit 1 | |
test ! -f $history2 && echo "File $history2 not found" && exit 1 | |
cat $history1 $history2 | awk -v date="WILL_NOT_APPEAR$(date +"%s")" '{if (sub(/\\$/,date)) printf "%s", $0; else print $0}' | LC_ALL=C sort -u | awk -v date="WILL_NOT_APPEAR$(date +"%s")" '{gsub('date',"\\\n"); print $0}' > $merged | |
echo "Merged to: $merged" |
I tried the builtin fc
method mentioned above but it doesn't sort the output! So in the resulting history you get all of one file first then all of the other file. Apparently zsh doesn't care that the timestamps are out of order and doesn't sort them. But the awk solution worked great for me!
Thanks, I was getting error with merge_history.sh:
awk: towc: multibyte conversion failure on: '�`|:"' ()[]{}<>\t"'
builtin worked like a charm. Thanks
Thank you!
Thanks to both of u guys
thank you, nice!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@borekb I assume these only work in an interactive shell
edit: yep: https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html