Table of Contents
How to use the sed command to delete lines between two patterns. In real situations, you will encounter many times to delete lines between two patterns. This article will guide you to use the sed command to do that job.
Situation required
I have the file mpm_prefork.conf, this is a configuration file of Apache2. Now, I want to delete all the lines in between the <Ifmodule> and </Ifmodule> tags. How to do it.

Delete lines between two patterns
The sed command structure to delete lines between two patterns (the command will not delete the two lines containing the pattern) that is:
sed -i '/pattern-1/,/pattern-2/{//!d}' filenameAccording to the above structure, I would write the command as follows:
sed -i '/<If/,/<\/If/{//!d}' mpm_prefork.conf- <If: this is the pattern-1.
- <\/If: this is the pattern-2. Why is there an
\in front of/. We need to add\to the sed command to talk that it is a character of the string to define. - {//!d}: delete all lines between two patterns, exclude 2 lines contain patterns.
Check the result
1. I run the command to view the file contents.
cat mpm_prefork.conf2. I run the command delete the line that I want.
3. Review the file content again.

In the image above, after running the sed command. You can see that the lines between the two tags we have defined have been removed.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).
Thanks. i will see.