更换SED两个已知串之间的串不明

编程入门 行业动态 更新时间:2024-10-24 08:24:32
本文介绍了更换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两个已知串之间的串不明

本文发布于:2023-11-02 17:43:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   SED

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!