首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将.zsh_history文件转换为.bash_history文件

将.zsh_history文件转换为.bash_history文件
EN

Stack Overflow用户
提问于 2015-11-11 17:53:25
回答 4查看 1K关注 0票数 4

最近我决定从zsh转到bash shell。有没有办法把我的.zsh_history文件快速转换成.bash_history文件?

.zsh_history代码段

代码语言:javascript
复制
: 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代码段

代码语言:javascript
复制
#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

请注意,命令可以跨越多行。

EN

回答 4

Stack Overflow用户

发布于 2015-11-11 18:40:26

奇怪的是,我的.bash_history中没有那些注释行,不管怎么说,我会用下面这样的代码

代码语言:javascript
复制
sed 's/^: \([0-9]*\):\w;\(.*\)$/#\1\n\2/' <.zsh_history >.bash_history

我远不是regexp方面的专家,所以这可能会做得更好,但我会解释它的作用。

代码语言:javascript
复制
 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
票数 3
EN

Stack Overflow用户

发布于 2017-04-29 01:47:16

在macOS上,我可以使用以下代码。它可以正确处理带有冒号的命令,但我不确定是否使用多行命令。

代码语言:javascript
复制
awk -F'[:;] *' '{ st = index($0,";");printf "#%s\n%s\n", $2, substr($0,st+1)}' ~/.zsh_history > ~/.bash_history
票数 3
EN

Stack Overflow用户

发布于 2015-11-11 19:46:41

这个awk脚本似乎做了正确的事情:

代码语言:javascript
复制
awk -F'[:;] *' '{printf "#%s\n%s\n", $2, $4}' file

字段分隔符定义为分号或冒号后跟任意数量的空格。然后打印感兴趣的两个字段,以及#和换行符。

在您的输入上进行测试:

代码语言:javascript
复制
$ cat file
: 1446994188:0;cat .bash_profile
: 1446994197:0;echo $shell
: 1446995957:0;vi ~/.zshrc
$ awk -F'[:;] *' '{printf "#%s\n%s\n", $2, $4}' file
#1446994188
cat .bash_profile
#1446994197
echo $shell
#1446995957
vi ~/.zshrc
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33648032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档