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.
A couple of notes in the above expression:
- The "-i" option tells Perl to perform in-place editing, meaning that Perl reads the input file, makes substitutions, and writes the results back to the same input file. If you want to dry-run the changes, you can omit this option, in which case the results will be shown to stdout without modifying the input file.
- The "-0" option turns Perl into "file slurp" mode, where Perl reads the entire input file in one shot (intead of line by line). This enables multi-line search and replace.
- The "-pe" option allows you to run Perl code (pattern matching and replacement in this case) and display output from the command line.
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).
Once confirming that the result is correct, you can apply the change to the input file by adding "-i" option:
Here are some variations:
To find a multi-line string pattern in a case-insensitive fashion:
To remove a multi-line string pattern:
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.
This one-liner searches for all text files in ~/doc folder, and apply multi-line string replacement in each file.
Subscribe to Ask Xmodulo
Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!