经过一番谷歌搜索后,我发现 sed 命令就是我所需要的。但说实话,这个命令的标志似乎无法理解。我想要做的就是用另一个文件替换单引号字符串。word1、word2 等等都不是单/双引号的。例子:
之前:foo.txt
word1 word2 word3 'This the text that needs replacement' word4 word5
Run Code Online (Sandbox Code Playgroud)
之后:foo.txt
word1 word2 word3 'I have been replaced' word4 word5
Run Code Online (Sandbox Code Playgroud)
请注意,文本“这是需要替换的文本”不是恒定的,其内容可能会有所不同。
只需将其替换为以下语法:
cat foo.txt | sed -e 's/want to be/have been/g'
Run Code Online (Sandbox Code Playgroud)
替换文件(例如在 mac 上)
sed -i '' 's/want to be/have been/g' foo.txt
Run Code Online (Sandbox Code Playgroud)
或其他unix
sed -i.bak 's/want to be/have been/g' foo.txt
Run Code Online (Sandbox Code Playgroud)