Skip to content

Instantly share code, notes, and snippets.

@calexandre
Last active April 3, 2024 14:11
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save calexandre/63547c8dd0e08bf693d298c503e20aab to your computer and use it in GitHub Desktop.
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"
@zhmurko
Copy link

zhmurko commented Nov 26, 2020

thanks!

@andrelaszlo
Copy link

Nice!

@NightMachinery
Copy link

zsh can do this itself:

    builtin fc -R -I "$hist_file"
    builtin fc -R -I "$another_hist_file"

    # write the loaded history to HISTFILE
    builtin fc -W "$HISTFILE"

@jumbosushi
Copy link

This was exactly what I needed. Thanks for sharing!

@djwaix
Copy link

djwaix commented Jan 3, 2022

Sweet! Thanks to both of you for providing options. I went with the builtin solution and it worked great.

@borekb
Copy link

borekb commented May 29, 2022

@NightMachinery This is great and works when I paste it to a terminal manually, however, it doesn't work for me when I create a script like merge-zhs-histories.sh. I tried this:

#!/usr/bin/env zsh

hist_file_1="/Users/borekb/.zsh_history"
hist_file_2="/Users/borekb/.zsh_history (1)"
hist_file_merged="/Users/borekb/.zsh_history-MERGED"

builtin fc -R -I "$hist_file_1"
builtin fc -R -I "$hist_file_2"

builtin fc -W "$hist_file_merged"

The .zsh_history-MERGED file is not created for me (but it is created when I copy/paste the code to my terminal).

Is that something specific about builtin fc? Or am I making some obvious mistake?

@martenson
Copy link

martenson commented Jun 3, 2022

@borekb I assume these only work in an interactive shell

edit: yep: https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html

@varenc
Copy link

varenc commented Sep 22, 2022

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!

@deeptshukla
Copy link

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

kutsan commented Apr 3, 2024

Thank you!

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