本文介绍了更换SED两个已知串之间的串不明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下内容的文件:
WORD1 WORD2 WORD3
我如何使用SED与富来代替WORD1和WORD3之间的字符串,例如,该文件的内容更改为以下内容:
WORD1富WORD3
我尝试以下,但显然我失去了一些东西,因为没有产生预期的结果:
SED -i'''S / WORD1。* WORD3 /富/ G'file.txt的
解决方案 SED -i的/ WORD1。* WORD3 / WORD1富WORD3 / G'file.txt的或
SED -i的/(WORD1)。*(WORD3)/ \\ 1富\\ 2 / G'file.txt的您可能需要逃避圆括号,取决于你的sed变种。
I have a file with the following contents:
WORD1 WORD2 WORD3
How can I use sed to replace the string between WORD1 and WORD3 with foo, such that the contents of the file are changed to the following?:
WORD1 foo WORD3
I tried the following, but obviously I'm missing something because that does not produce the desired results:
sed -i '' 's/WORD1.*WORD3/foo/g' file.txt
解决方案 sed -i 's/WORD1.*WORD3/WORD1 foo WORD3/g' file.txtor
sed -i 's/(WORD1).*(WORD3)/\1 foo \2/g' file.txtYou might need to escape round brackets, depends on your sed variant.
更多推荐
更换SED两个已知串之间的串不明
发布评论