How to search and replace a multi-line string in a file on Linux

Last updated on July 4, 2020 by Dan Nanni

Question: I have a text file in which I want to change multiple lines of text to something else, but without using a text editor. Is there a way to find and replace a multi-line string pattern from the Linux command line?

Suppose you have a text file that looks like the following.

Beginning of a text file.

This is a cat.
This is a dog.
This is a guinea pig.
This is a gecko.
This is a hamster.
This is a parrot.
This is a rabbit.
This is a ferret.
This is a turtle.

You want to replace the highlighted multiple lines with the following one-line, but without using a text editor.

Beginning of a text file.

This is a cat.
This is a dog.
These were removed.
This is a parrot.
This is a rabbit.
This is a ferret.
This is a turtle.

Search and Replace a Multi-line String Pattern

When it comes to finding and replacing a multi-line string pattern, Perl's regular expression based pattern matching comes in handy.

The following one-liner can get the job done.

$ perl -i -0pe 's/<multi-line-string-pattern>/<replacement-string>/' input.txt

A couple of notes in the above expression:

In this particular example, the following command will replace a multi-line string as required, and display the result (without applying the change to the input file).

$ perl -0pe 's/This is a guinea pig.nThis is a gecko.nThis is a hamster.n/These were removed.n/' input.txt

Once confirming that the result is correct, you can apply the change to the input file by adding -i option:

$ perl -i -0pe 's/This is a guinea pig.nThis is a gecko.nThis is a hamster.n/These were removed.n/' input.txt

Here are some variations:

To find a multi-line string pattern in a case-insensitive fashion:

$ perl -i -0pe 's/<multi-line-string-pattern>/<replacement-string>/i' input.txt

To remove a multi-line string pattern:

$ perl -i -0pe 's/<multi-line-string-pattern>//' input.txt

Search and Replace a Multi-line String in Multiple Files

If you want to find and replace a multi-line string in more than one files, you can use a combination of find and xargs as follows.

$ find ~/doc -name "*.txt" | xargs perl -i -0pe 's/<multi-line-string-pattern>/<replacement-string>/'

This one-liner searches for all text files in ~/doc folder, and apply multi-line string replacement in each file.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean