简体   繁体   English

命令行 - 从上一个命令替换字符串并执行

[英]Command line—Replace string from previous command and execute

I'd like to know the shortest command for correcting a mistake in the previously executed command. 我想知道用于纠正先前执行的命令中的错误的最短命令。

Given I executed the following command 鉴于我执行了以下命令
cd /Users/USERNAME/Library/Preferences/ByHost

I would like to be able to execute a new command that takes the previous command, pipes it through grep or a similar Unix tool, and then executes. 我希望能够执行一个新命令,它接受上一个命令,通过grep或类似的Unix工具管道,然后执行。 Something like this in (my admittedly uneducated) psuedo-command. 在(我公认的未受过教育的)伪造的命令中有类似的东西。
!! | xargs 's/$1/USERNAME/cirrostratus/g'

This command would execute cd /Users/cirrostratus/Library/Preferences/ByHost . 该命令将执行cd /Users/cirrostratus/Library/Preferences/ByHost

Alternately, piping a string, searching and replacing on it and executing in one line would be my second choice. 或者,管道一个字符串,搜索和替换它并在一行中执行将是我的第二选择。

If you wanna replace a all occurences of a string (switch g ) you'll need to write: 如果你想要替换一个字符串的全部出现(开关g ),你需要写:

!!:gs/oldstring/newstring/

This will replace oldstring by newstring in you're last command and run them and store the result to history. 这将在你最后一个命令中用newstring替换oldstring 运行它们并将结果存储到历史记录中。

Otherwise: then syntaxe ^oldstring^newstring^ is a shortcut for: 否则:然后syntaxe ^oldstring^newstring^是一个快捷方式:

!!:s/oldstring/newstring/

replacing only first found occurence of string. 仅替换首次发现的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM