更换SED两个已知串之间的串不明 [英] replace a unknown string between two known strings with sed

查看:91
本文介绍了更换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.txt

or

sed -i 's/(WORD1).*(WORD3)/\1 foo \2/g' file.txt

You might need to escape round brackets, depends on your sed variant.

这篇关于更换SED两个已知串之间的串不明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆