Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .zsh_history file to .bash_history file

Tags:

bash

zsh

Recently I decided to move from zsh to bash shell. Is there a way to quickly convert my .zsh_history file to .bash_history file?

.zsh_history snippet

: 1446994188:0;cat .bash_profile
: 1446994197:0;echo $shell
: 1446995957:0;vi ~/.zshrc
: 1437893246:0;curl -fLo ~/.vim/autoload/plug.vim --create-dirs \\
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

.bash_history snippet

#1446994188
cat .bash_profile
#1446994197
echo $shell
#1446995957
vi ~/.zshrc
#1437893246
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Please note that command can span multiple lines.

like image 447
sdayal Avatar asked Mar 14 '23 05:03

sdayal


1 Answers

It's strange, I don't have those commented lines in my .bash_history, anyway, I would use something like this

sed 's/^: \([0-9]*\):\w;\(.*\)$/#\1\n\2/' <.zsh_history >.bash_history

I'm far from being an expert in regexps, so that might be done better, but I'll explain what it does.

 sed 's/^ -- start of the line
      :   -- tell the regexp there's a ":"
      \([0-9]*\) -- identify a series of numbers and store that in 1
      :\w; -- another ":" followed by a word and by ";" 
      \(.*\)$ -- store whatever you find until the end of the line in 2
      /#\1\n\2/' -- print what you have in 1 on a line and what you have in 2 on the next
like image 147
Alberto Zaccagni Avatar answered Mar 23 '23 22:03

Alberto Zaccagni