Skip to content

Instantly share code, notes, and snippets.

@pstaender
Last active August 25, 2019 05:22
Show Gist options
  • Save pstaender/87eee46dfdc5684da4cf0ada22779b25 to your computer and use it in GitHub Desktop.
Save pstaender/87eee46dfdc5684da4cf0ada22779b25 to your computer and use it in GitHub Desktop.
Usage: ruby merge_zsh_history_and_remove_duplicates.rb ~/.zsh_history_1 ~/.zsh_history_2 …
#!/bin/env rubyn
remove_duplicates = true
cmds = []
exclude = ['exit', /^(ls|l|ll)\s/, /^(ls|l|ll)$/]
history = {}
ARGV.each do |file|
File.readlines(file).each do |line|
time, cmd = line.encode('UTF-8').encode('UTF-8', invalid: :replace).split(';')
timestamp = time.split(':')[1]&.strip || ''
cmd ||= ''
cmd.strip!
timestamp.strip!
next if timestamp.empty? || cmd.empty?
next if exclude.any? && exclude.map { |pattern|
cmd.match?(pattern) || nil
}.reject(&:nil?).any?
if remove_duplicates
if cmds.include?(cmd)
next
else
cmds << cmd
end
end
history[timestamp.to_i] ||= []
history[timestamp.to_i] << cmd
end
end
history.sort.each do |timestamp, cmds|
cmds.each_with_index do |cmd, i|
puts ": #{timestamp}:#{i};#{cmd}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment