我需要从 Bash 命令行替换文件中的 2 个单词,例如: 文件A.txt
xxxx AA BB xxx
我需要将 Bash 命令行中的“AA BB”替换为“CC DD”,因此文件将变为:
xxxx CC DD xxx
感谢您的帮助!
最佳答案
您可以尝试以下sed
命令,
sed -i 's/AA BB/CC DD/g' file
示例:
$ echo 'xxxx AA BB xxx' | sed 's/AA BB/CC DD/g'
xxxx CC DD xxx
通过 awk,
awk '{sub(/AA BB/,"CC DD")}1' infile > outfile
示例:
$ echo 'xxxx AA BB xxx' | awk '{sub(/AA BB/,"CC DD")}1'
xxxx CC DD xxx
关于Linux bash : how to replace 2 words in a file from bash command line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358367/