在 Bash shell 中搜索和替换插入符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2149482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caret search and replace in Bash shell
提问by mattjames
If I have just entered the following command in Bash:
如果我刚刚在 Bash 中输入了以下命令:
echo foo
I can change foo to bar by typing:
我可以通过键入以下内容将 foo 更改为 bar:
^foo^bar
Which results in the following command being executed:
这导致执行以下命令:
echo bar
Now if I enter:
现在,如果我输入:
echo foo foo
Is there a way to change both instances of foo to bar just by using the caret (^) operator?
有没有办法仅使用插入符号 ( ^) 运算符将 foo 的两个实例更改为 bar ?
Additionally, are there man pages for shell operators like ^? man ^results in "No manual entry for ^".
此外,是否有 shell 操作员的手册页,例如^?man ^导致“没有手动输入 ^”。
回答by Adam Rosenfield
That particular feature is called quick substitution; its documentation can be found in the Event Designatorssection of the Bash Manual. You can't do what you want with quick substitution; you'll have to resort to something slightly more verbose:
该特定功能称为快速替换;它的文档可以在 Bash 手册的事件指示符部分找到。你不能用快速替换来做你想做的事;你将不得不求助于稍微更冗长的东西:
!!:gs/foo/bar/
回答by R Samuel Klatchko
Nor sure how to do it with caret substitution, but here's how you do it with history:
也不知道如何用插入符号替换来做到这一点,但这里是你如何用历史来做到这一点:
!!:gs/foo/bar/
Let me break that down:
让我分解一下:
!! - reruns the last command. You can also use !-2 to run two commands ago, !echo to run the last command that starts with echo
:gs says to do a global (all instances) search/replace. If you wanted to just do replace the first instance, you would use ':s'
Finally, /foo/bar/ says to replace foo with bar
!! - 重新运行最后一个命令。您也可以使用 !-2 运行前两个命令, !echo 运行最后一个以 echo 开头的命令
:gs 说要进行全局(所有实例)搜索/替换。如果您只想替换第一个实例,您可以使用 ':s'
最后,/foo/bar/ 说用 bar 替换 foo
回答by FlatEarther
Try:
尝试:
^foo^bar^:&
As you know ^foo^bar^performs just one substitution, and the :&modifier repeats it.
如您所知,^foo^bar^仅执行一次替换,:&修饰符会重复执行。
回答by Ignacio Vazquez-Abrams
Caret substitution and other similar shortcuts are found in the Event Designatorssubsection of the HISTORY EXPANSIONsection of the bash(1)man page.
在手册页的HISTORY EXPANSION部分的Event Designators小节中可以找到插入符号替换和其他类似的快捷方式。bash(1)
回答by SergioAraujo
^word^ ........... erase word
^word^^ ........... delete everything until the end of the line
回答by MLP
If you're looking for something less difficult to memorize that accomplishes the same thing as the above !!:gs/foo/bar/, you could always create a function in your .bash_profile start-up script. I chose replace().
如果您正在寻找一些不太难记住的东西来完成与上述相同的事情!!:gs/foo/bar/,您总是可以在您的 .bash_profile 启动脚本中创建一个函数。我选择了replace()。
replace() {
eval $(echo $(fc -ln -1) | eval "sed 's///g'") #compact form
}
OR, Less convolutedly
或者,不那么复杂
replace() {
string=$(fc -ln -1) #gets last command string
repcmmd="sed 's///g'" #build replacement sed command from fn input
eval $(echo $string | eval $repcmmd) #evaluates the replacement command
}
Then the replace all can be made with
然后替换所有可以用
echo foo foo
replace foo bar

