Skip to content

Instantly share code, notes, and snippets.

@calexandre
Last active February 14, 2026 16:05
Show Gist options
  • Select an option

  • Save calexandre/63547c8dd0e08bf693d298c503e20aab to your computer and use it in GitHub Desktop.

Select an option

Save calexandre/63547c8dd0e08bf693d298c503e20aab to your computer and use it in GitHub Desktop.
Merge two zsh history files
#!/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"
@deeptshukla
Copy link
Copy Markdown

Thanks, I was getting error with merge_history.sh:
awk: towc: multibyte conversion failure on: '�`|:"' ()[]{}<>\t"'

builtin worked like a charm. Thanks

@kutsan
Copy link
Copy Markdown

kutsan commented Apr 3, 2024

Thank you!

@CapedBaldy99
Copy link
Copy Markdown

Thanks to both of u guys

@shixpGit
Copy link
Copy Markdown

shixpGit commented Jul 9, 2024

thank you, nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment