It took me a while to figure out how to do this, so posting in case anyone else is looking for the same.
-
[Insert blank line in UNIX after different number of lines](https://unix.stackexchange.com/q/222810/56041) on [Unix & Linux Stack Exchange](http://unix.stackexchange.com/). – jww Jan 15 '18 at 00:55
6 Answers
For adding a newline after a pattern, you can also say:
sed '/pattern/{G;}' filename
Quoting GNU sed manual:
G
Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.
EDIT:
Incidentally, this happens to be covered in sed one liners:
# insert a blank line below every line which matches "regex"
sed '/regex/G'

- 118,548
- 33
- 236
- 227
-
`sed '/\n/G' filename` doesn't work. But `sed '/e/G' filename` works. – Song Yang Nov 25 '21 at 23:39
This sed command:
sed -i '' '/pid = run/ a\
\
' file.txt
Finds the line with: pid = run
file.txt before
; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid
; Error log file
and adds a linebreak after that line inside file.txt
file.txt after
; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid
; Error log file
Or if you want to add text and a linebreak:
sed -i '/pid = run/ a\
new line of text\
' file.txt
file.txt after
; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid
new line of text
; Error log file

- 569
- 1
- 4
- 6
-
1`sed 'a'` is a pretty good idea ... I would've used `sed -e '/pid = run/ s/$/\n/'` or `sed -e '/pid = run/ s/$/\nnew line of text/'` – Uroc327 Jun 28 '13 at 11:03
A simple substitution works well:
sed 's/pattern.*$/&\n/'
Example :
$ printf "Hi\nBye\n" | sed 's/H.*$/&\nJohn/'
Hi
John
Bye
To be standard compliant, replace \n by backslash newline :
$ printf "Hi\nBye\n" | sed 's/H.*$/&\
> John/'
Hi
John
Bye

- 992
- 1
- 11
- 27
-
4To answer the question as asked, you'd have to do `sed 's/pattern.*/&\n/'`, otherwise you'll insert the newline right after the match instead of at the end of the line. Also note that your solution requires _GNU_ `sed` (won't work on BSD/OSX, because the BSD `sed` implementation doesn't support escape sequence `\n` in the _replacement string_). – mklement0 Jun 06 '16 at 02:29
-
1Oh thanks I didn't know about the difference between GNU sed and BSD/OSX sed. Worth knowing ! – Richard Jun 06 '16 at 04:23
-
1++; there are many differences, unfortunately; BSD Sed implements only a few extensions to the POSIX spec., while GNU Sed implements many more - I've tried to compile the differences in [this answer](http://stackoverflow.com/a/24276470/45375). – mklement0 Jun 06 '16 at 04:32
-
With GNU sed and ripgrep (rg) in a terminal this worked for me: `rg . --color always -in -e 'apples' |sort | sed 's|\./|======================\n|'` to separate rg results lines, where the raw results begin with `./10643.html:...` – Victoria Stuart Mar 22 '23 at 17:39
sed '/pattern/a\\r' file name
It will add a return after the pattern while g
will replace the pattern with a blank line.
If a new line (blank) has to be added at end of the file use this:
sed '$a\\r' file name

- 31
- 1
-
2CARRIAGE RETURN vs LINE FEED: some people will need to use a `\n` instead of a `\r`. – sjas Dec 04 '17 at 17:35
Another possibility, e.g. if You don't have an empty hold register, could be:
sed '/pattern/{p;s/.*//}' file
Explanation:
/pattern/{...}
= apply sequence of commands, if line with pattern found,
p
= print the current line,
;
= separator between commands,
s/.*//
= replace anything with nothing in the pattern register,
then automatically print the empty pattern register as additional line)

- 587
- 5
- 12
The easiest option -->
sed 'i\
' filename

- 7
- 3
-
every other answer interpret the question as 'inserting empty line after a **pattern matched line**'. This answer does not even pattern match. – Apiwat Chantawibul Apr 28 '23 at 01:21