sed 替换部分字符串 [英] sed to replace partial string
本文介绍了sed 替换部分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 sed 执行以下操作
I would like to do the following by using sed
情况 1:
*here is some random text constant=randomValue some more random text*
我想直接在 constant= 之后替换随机值,同时保持该行的其余部分完好无损,因此结果如下所示:
I would like to replace randomvalue directly after constant= while keeping the rest of the line intact so the result would look like:
*here is some random text constant=substituteValue some more random text*
案例 2:
*here is some random text constant= randomValue some more random text*
与第一个类似,但在 constant= 字符串后有一个空格,因此输出将是
similar to the first one but with a space after the constant= string, so the output would be
*here is some random text constant= substituteValue some more random text*
对于我一直在尝试使用的案例一:
For case one i've been trying to use:
echo $line | sed 's/server=.*\ /sever=\<SrvrName\>/'
推荐答案
你可以使用这个 sed:
You can use this sed:
sed 's/\( constant = *\)[^ ]*/\1substituteValue/' <<< "$line"
这篇关于sed 替换部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文