0

I have a file that has data something like

.spec.nodes.brokers.runtime.properties.broker.http.numConnections=15
.spec.nodes.clients.runtime.properties.broker.http.numConnections=17
.spec.nodes.servers.runtime.properties.broker.http.numConnections=19

I want to replace runtime.properties by '"runtime.properties"'. I can manually do multiple sed, but I wanted to see if I can get it done by one sed using pattern matching.

Also, I cannot just directly sed and replace "runtime.properties" because there are other patterns that might match this keyword. I need something that matches .spec.nodes.<one of brokers, clients or servers>, then replace

.spec.nodes should only be at the start of the line, and not anywhere in the string.

1
  • So, do you want to change runtime.properties only if it’s immediately preceded by .spec.nodes.XXXXXXs., or if .spec.nodes.XXXXXXs. appears anywhere on the same line? … … … … … … … … … Please do not respond in comments; edit your question to make it clearer and more complete. It’s best (a) to show expected/desired output, and (b) to include enough variation in your example input to cover all noteworthy cases (e.g., .spec.nodes.sellers.runtime.properties.broker.http, .spec.nodes.brokers.foo.runtime.properties.broker.http. and .spec.nodes.brokers.runtime.properties.brokers.http.). Jul 28, 2021 at 19:07

1 Answer 1

1

Given

$ cat file
.spec.nodes.brokers.runtime.properties.broker.http.numConnections=15
.spec.nodes.foo.runtime.properties.broker.http.numConnections=17
.spec.nodes.clients.runtime.properties.broker.http.numConnections=17
.spec.nodes.bar.runtime.properties.broker.http.numConnections=17
.spec.nodes.servers.runtime.properties.broker.http.numConnections=19

then

$ sed -E "/\.spec\.nodes\.(brokers|clients|servers)/s/runtime\.properties/'\"&\"'/" file
.spec.nodes.brokers.'"runtime.properties"'.broker.http.numConnections=15
.spec.nodes.foo.runtime.properties.broker.http.numConnections=17
.spec.nodes.clients.'"runtime.properties"'.broker.http.numConnections=17
.spec.nodes.bar.runtime.properties.broker.http.numConnections=17
.spec.nodes.servers.'"runtime.properties"'.broker.http.numConnections=19

or

$ sed -E '/\.spec\.nodes\.(brokers|clients|servers)/s/runtime\.properties/'\''"&"'\''/'
file
.spec.nodes.brokers.'"runtime.properties"'.broker.http.numConnections=15
.spec.nodes.foo.runtime.properties.broker.http.numConnections=17
.spec.nodes.clients.'"runtime.properties"'.broker.http.numConnections=17
.spec.nodes.bar.runtime.properties.broker.http.numConnections=17
.spec.nodes.servers.'"runtime.properties"'.broker.http.numConnections=19
3
  • Its working, thanks. I also need a single quotes. '"runtime.properties"'Trying that out now.
    – CSUNNY
    Jul 27, 2021 at 18:47
  • This works sed -E '/\.spec\.nodes\.(brokers|clients|servers)/s/runtime\.properties/'"'"'"&"'"'"'/g'
    – CSUNNY
    Jul 27, 2021 at 18:51
  • @CSUNNY apologies, I missed that - see updated answer Jul 27, 2021 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .