I am new to using sed but I have been experimenting sed's s/..../..../
(substitute) to modify a full sentence if it's on one line but I am unaware an alternative solution on how to modify a sentence that may have been separated on two lines such as:
This:
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
is actually written as this:
Lorem Ipsum is simply dummy text of the
printing and typesetting industry.
How can you detect for this or code it to replace the entire sentence even if it's on two lines instead of one?
The following command will work in both situations:
More explanations: How can I use sed to replace a multi-line string?
While sed can match patterns on multiple lines (using the commands
N
orH
to append successive lines before matching), this is well outside its comfort zone. Attempt it only if you like pain.Perl can do this kind of things nicely. Use the
-p
switch to make it process standard input one record at a time and print the modified record (à la sed), and-000
to turn on paragraph mode (where records are separated by blank lines). In a regular expression,\s
matches any whitespace character including a newline.If you want to put a newline in the replacement text when the original contains one, that's more complicated. How to do it depends on your requirements as to where to put the newline.
sed cannot easily read across multiple lines. Use
perl -i -0pe 's/.../.../...'
instead.