0

I'd like to paste three files into a single file with two spaces between each file using the paste command in linux bash:

paste -d '\n\n' file1.txt file2.txt file3.txt > output.txt

However, there is no difference at all between paste -d '\n' and paste -d '\n\n'.

Let's say file1 consists of the single number "1", file2 of "2" and file3 of "3", I always get the following:

1
2
3

But I'd like to have:

1

2

3

Why does my code not work?

0
5

Only one character from the -d option's argument list is used to replace each newline character from the input. Quoting the POSIX documentation for the utility (bold mine):

-d list
... These elements specify one or more delimiters to use, instead of the default <tab>, to replace the <newline> of the input lines. The elements in list shall be used circularly; that is, when the list is exhausted the first element from the list is reused. ...

To achieve what you are looking for (without switching to different tools that, depending on your real use case, might be more effective) you can use:

paste -d '\n' file1.txt /dev/null file2.txt /dev/null file3.txt /dev/null

(Note that, compared to your expected result, this also adds a final newline you may not want).

1
  • 3
    See also :|paste -d '\n' file1.txt - file2.txt - file3.txt - – Stéphane Chazelas Apr 27 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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