Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed to replace partial string

I would like to do the following by using sed

case 1:

*here is some random text constant=randomValue some more random text*

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*

case2:

*here is some random text constant= randomValue some more random text*

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\>/'
like image 585
user2967606 Avatar asked Jan 15 '14 19:01

user2967606


1 Answers

You can use this sed:

sed 's/\( constant = *\)[^ ]*/\1substituteValue/' <<< "$line"
like image 141
anubhava Avatar answered Oct 16 '22 20:10

anubhava