パターンを変更するには、複数の単語を一致させます。

パターンを変更するには、複数の単語を一致させます。

次のデータを含むファイルがあります。

.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

runtime.propertiesに交換したいです'"runtime.properties"'。複数のsedを手動で実行できますが、パターンマッチングを使用して1つのsedで実行できることを確認したいと思います。

また、このキーワードと一致できる他のパターンがあるため、「runtime.properties」をsedして置き換えることはできません。何かを一致させて.spec.nodes.<one of brokers, clients or servers>, から交換する必要があります

.spec.nodes文字列のどこにも存在できず、行の先頭にのみ存在できます。

ベストアンサー1

与えられた

$ 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

それから

$ 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

または

$ 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

おすすめ記事