leviathan747
leviathan747

Reputation: 1

Multi-line search with sed

I need to append a few lines to a configuration file. The format is something like follows:

[Topic1]
param=foo
param=bar
param=foobar

[Topic2]
param=one
param=two

etc...

I am trying to write a script using sed to append parameters to a specific topic. Since all the topics have param=, I can't just insert a line after the last occurrence of that string. Also, I can't count on the value of the last parameter being consistent so for example I can't just insert a line after the string param=two

Any help would be appreciated. I'm not too familiar with mutliline sed-fu. Thanks!

Upvotes: 0

Views: 113

Answers (1)

J.Z
J.Z

Reputation: 419

sed -i -r ':a; N; $!ba; s/\[Topic1\]\n(param=[a-zA-Z]*\n)*/&param=VALUE\n/g' FILE_NAME

Basically what :a; N; $!ba; doing is append all line when not the last line (N) to the tag created by :a so that we can use \n in our expression.

Then match [Topic1] followed by arbitrary number of param=xxx, and append param=VALUE to the end of the matching result (&).

Upvotes: 1

Related Questions