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!!
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.
In the famous sed one-line blog (http://www.catonmat.net/blog/wp-content/uploads/2008/09/sed1line.txt)
# insert a blank line above every line which matches "regex"
sed '/regex/{x;p;x;}'
so in your case,
sed '/atterntosearch4/{x;p;p;p;p;x}' file
or
sed '/atterntosearch4/{x;P;P;P;P;x}' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With