Question

In the Riak app.config file there is a section that looks like this:

%% JMX Config
  {riak_jmx, [
     {enabled, false}
  ]},

I want to substitute it for this:

%% JMX Config
  {riak_jmx, [
     {enabled, true}
  ]},

Yeah, pretty trivial, but I've wanted to be able to do this for ages, multi-line text replacement. I'd like to use AWK or Sed to perform the operation.

(It's worth mentioning that I'm on OSX so I don't have the fancy GNU versions of those programs).

The closest I've gotten with either is the following, which don't really work properly...

 sed '/{riak_jmx/,/]},/{ c\
     \{riak_jmx, [\
         \{enabled, false\}

     ;   }' ./app.config

And:

awk '/{riak_jmx/,/]},/{ if($0  ~ "{enabled, false}") {
      print "{enabled, true}" }
     else {
       print $0
     } }1' < ./app.config

Which doesn't work either. Both these commands print the old, and new version of the block.

Any ideas?

Was it helpful?

Solution

try using sed's s command:

sed '/{riak_jmx/,/]},/{ s/enabled, false/enabled, true/;}' ./app.config

OTHER TIPS

Using awk you can do:

awk '/riak_jmx/,/]/ {sub(/false/,"true")}1' app.config
%% JMX Config
  {riak_jmx, [
     {enabled, true}
  ]},
awk '/{riak_jmx, \[/ {print; getline; if($0 ~ /{enabled, false}/) sub(/false/,"true")}{print}' app.config

Output:

%% JMX Config
  {riak_jmx, [
     {enabled, true}
  ]}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top