How to insert 4 newlines before pattern match with SED?

macos, sed, shell, terminal

Solution

This might work for you (GNU sed):

sed '/patterntosearch4/i\\n\n\n' file

or if you prefer:

sed -e '/patterntosearch4/!b' -e 'G' -e 's/\(.*\)\(.\)/\2\2\2\2\1/' file

BTW as you use the `G` command, which inserts a newline following the line with the pattern match, I guessed you wanted a newline inserted before the line with the pattern match.

Problem

How to enter 4 blanklines before pattern matches? I can do ``` sed '/patterntosearch4/G' 1.txt > 2.txt ``` to create a new file (2.txt) with a blank line AFTER each pattern. But how to enter 4 blank lines BEFORE match? Manpage SED didnt seem to provide any answers. Any help is much appreciated!!

Original source