0

I have the following text:

client_encryption_options:
    enabled: true
    # If enabled and optional is set to true encrypted and unencrypted connections are handled.
    optional: false
    keystore: conf/.keystore
    keystore_password: cassandra

I am trying to change the value in the keystore under client_encryption_options using the sed command.

sed "/^client_encryption_options:/,+1s/keystore:.*/keystore: \/opt\/test/" $CASSANDRA_YAML_FILE > cassandra.yaml.tmp && mv cassandra.yaml.tmp $CASSANDRA_YAML_FILE

When I try the above command it doesn't replace the conf/.keystore with /opt/test/

user1692342
  • 127
  • 1
  • 2
  • 7

2 Answers2

2

No, it doesn't.

You're using my solution from a previous question. That question specifically asks about replacing a value on the next line. This question requires you to make the change four lines further down.

Changing the +1 to +4 may fix it for you.

Kusalananda
  • 333,661
  • What if i Want to search for the particular text. If the order changes that is – user1692342 Feb 15 '17 at 15:49
  • 1
    @user1692342 The substitution will be applied to all four lines (actually five, the one matching ^client_encryption_options: and +4 more). As long as the pattern is unique, it will change the wanted line, no matter where amongst the four lines it is. – Kusalananda Feb 15 '17 at 15:52
0
sed -e '
   /^client_encryption_options:/,/keystore:/!b
   //!b
   s|\(keystore:\).*|\1 /opt/test|
' < $CASSANDRA_YAML_FILE > cassandra.yaml.tmp && \
   mv cassandra.yaml.tmp "$CASSANDRA_YAML_FILE"

You can even use the '-i' (inplace edit) option if your 'sed' supports it to do away with the 'mv':

sed -i.BAK -e 'your edit commands here' $_YOUR_YAML_FILE # Note: NO redirections here