gpt4 book ai didi

Linux bash : how to replace 2 words in a file from bash command line

转载 作者:太空宇宙 更新时间:2023-11-04 05:57:32 24 4
gpt4 key购买 nike

我需要从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com