This question already has an answer here:

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

    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?

marked as duplicate by don_crissti, Tomasz, Jeff Schaller, dhag, GAD3R Feb 14 '17 at 19:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


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:).


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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.