1

I have a pattern in a file which includes a newline:

client_encryption_options:
    enabled: false

I want to set enabled to true. But the enabled has to be the one below client_encryption_options.

grep doesn't allow me to search for multiline. How can I achieve this?

dhag
  • 15,736
  • 4
  • 55
  • 65
user1692342
  • 127
  • 1
  • 2
  • 7

2 Answers2

4

Well, grep would additionally not allow you to make modifications, so that utility is out of the picture from the start.

Using GNU sed instead:

$ sed '/^client_encryption_options:/,+1s/enabled:.*/enabled: true/' file

This will find the line starting with the string client_encryption_options: and will apply a substitution to it and the following line. The substitution will replace the string enabled: and everything following it on the same line with enabled: true.

The substitution will be applied to both lines, but since the pattern enabled:.* isn't found on the first line, it will remain unchanged. The second line will be changed unconditionally (regardless of the text after enabled:).

Kusalananda
  • 333,661
3

With sed:

sed '/client_encryption_options:/{n;s/false/true/;}'

n is the command to get the next line into the pattern space (after having printed and discarded the current pattern space content), s is to substitute.