12

This question already has an answer here:

When I have an output stream:

a
b
c
d
e

How can I double the newlines:

a

b

c

d

e

marked as duplicate by don_crissti, Jeff Schaller, telcoM, cas, Romeo Ninov Jan 25 '18 at 8:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

21
sed G

is a well known one-liner for that.

Performance-wise, the most effective with the standard Unix tool chest would probably be:

paste -d '\n' - /dev/null

If you don't want to add an empty line after the last line:

sed '$!G'

To add the empty lines before the input lines:

paste -d '\n' /dev/null -

Or:

sed 'i\
/'
  • To complete the list awk 'BEGIN{ORS="\n\n"}1' file :) – Valentin Bajrami Nov 3 '14 at 21:40
5

using pr: Paginating Files

pr -t -d file.txt

output:

a

b

c

d

e

using awk:

awk '{printf("%s\n\n",$0)}' file.txt
  • This has already been proposed by @don_crissti – Anthon Nov 3 '14 at 21:23
  • @StéphaneChazelas don_crissti's answer was only gone for 5 minutes and restored (with the pipe) 15 minutes before the original (pr only) answer here appeared, hence my remark. (deleted 2014-11-03 21:00:34Z, restored 2014-11-03 21:05:34Z first version of this answer: 2014-11-03 21:21:44Z) – Anthon Nov 4 '14 at 4:10
4

With awk you can define the Output Record Separator as double new line:

awk -v ORS="\n\n" 1 file

Then, 1 performs the default awk action: {print $0}, that is, print the current line.

4

While sed is the obvious choice, another very reliable candidate might be dd.

It is probably more useful for situations in which it is the byte-count that matters more than it is the line-count, but blocking and unblocking data is what it does best:

seq 10| dd cbs=16 conv=block                             
1               2               3               4               5               6               7               8               9               10              

That, so far, is more like achieving the opposite of what you've asked, but dd can block lines on spaces - it will translate input \newlines into spaces and pad those lines on cbs=[num] bytes of spaces. And so if you can reliably predict a maximum byte-length per line (because guessing too low will result in truncation), and then double that figure...

seq 10| dd cbs=16 conv=block | dd cbs=8 conv=unblock

1

2

3

4

5

6

7

8

9

10

...you can then unblock it on half the cbs - which will trim trailing spaces and print a \newline every cbs=[num] bytes. This is much more useful for handling large volumes of possibly binary data (or for terminal input) than it would be for ordinary text files, but it is an alternative - and can definitely compete (on the large scale) in the performance department with just about anything.

Another specialized alternative might be nl and tr. This would only be useful if your input resembles that shown - because the solution involves translating spaces into \newlines. In any case, nl will always insert at least -width +1 bytes of spaces at the head of each line it is passed - whether or not it numbers them. And so you can...

printf %s\\n a b c d e |
nl -bn |
tr -s \  \\n

...which prints:


a

b

c

d

e

The -bn switch instructs nl not to number the body of its input, but it still inserts 6 spaces by default - and at least two even with a -w1 argument. This can make it useful for other cases as well - such as...

INPUT |nl -bn -w1 |sed '/address/s/  //...' | sed '/^  /!...'

...in which the second sed always knows which lines the first has touched. But I digress...

Anyway, tr -squeezes all incoming sequences of spaces into a single byte and translates each result into a \newline. Without the -s switch there would be six new intervening \newlines between each alphabetic above. And that can be reliably set as easily as -w1.

Still, I would use sed. And speaking of which, though the Get commands and insert commands have already been mentioned, there are also the append and change commands for that. Doing either would look like this:

printf %s\\n a b c d e|
sed 'a\
'
a

b

c

d

e

sed G works because it Gets the contents of the Hold buffer appended to the pattern buffer following a \newline. Hold space is, by default, empty, and so all you get is a \newline. But, in case there is more to the sed script and the Hold buffer is not empty, then you can always append any string at all to any line, or else you can print the line then change it on output like:

INPUT| sed '...;p;c\
'

...with GNU sed the actual newline is not required for i, c, or a and all three can be used like sed '[aic]\\' though maybe that is a bad habit to adopt. In any case, any one of those three is likely less costly performance-wise than a s///ubstitution because none imply a regex pattern, though I doubt if the difference is at all significant.

3

You can pipe through sed:

.... | sed 's/$/\n/'
  • 1
    Is it just me, or you've really asked the question just to answer it by yourself? :D – Eenoku Nov 3 '14 at 20:45
  • 3
    @MartinBeseda yes, the question came up on chat and was claimed to already been answered. Answering own questions is perfectly valid. – Anthon Nov 3 '14 at 20:50
  • 6
    sed G would be more canonical - see Famous sed one-liners explained – steeldriver Nov 3 '14 at 20:51
  • @steeldriver put that up as an answer and I'll accept it. – Anthon Nov 3 '14 at 20:53
  • 1
    That is not standard syntax. On the right hand side of the s command, portably (and POSIXly), you need a backslash followed by a literal newline character. – Stéphane Chazelas Nov 3 '14 at 20:57
3

Using perl you can add a newline before

...|perl -pe 's/^/\n/'

or after each line:

...|perl -pe 's/$/\n/'

Not the answer you're looking for? Browse other questions tagged or ask your own question.