Not how to insert a newline before a line. This is asking how to insert a newline before a pattern within a line.
For example,
sed 's/regexp/&\n/g'
will insert a newline behind the regexp pattern.
How can I do the same but in front of the pattern?
Here is an example input file
somevariable (012)345-6789
Should become
somevariable
(012)345-6789
To insert a newline to output stream on Linux, I used:
Where
file1
was:Before the sed in-place replacement, and:
After the sed in-place replacement. Please note the use of
\\\n
. If the patterns have a"
inside it, escape using\"
.\0 is the null, so your expression is replaced with null (nothing) and then...
\n is the new line
On some flavors of Unix doesn't work, but I think it's the solution at your problem.
in sed you can reference groups in your pattern with "\1", "\2", .... so if the pattern you're looking for is "PATTERN", and you want to insert "BEFORE" in front of it, you can use, sans escaping
i.e.
After reading all the answers to this question, it still took me many attempts to get the correct syntax to the following example script:
The script appends a newline
\n
followed by another line of text to the end of a file using a singlesed
command.