Last Updated: February 25, 2016
·
549
· b4hand

Use paste -d "\n" to merge files line by line

If you've ever used split -n with the round-robin distribution and wondered how to merge the files back together in the right order, you can use paste.

Let's say you've generated some files using the -nr/X style of split:

seq -w 1 9 | split -nr/3 -d - parts.

If you've never heard of the round-robin distribution for split, it is a bit obscure, but is very handy at times. I highly recommend you read more about it in the split documentation which gives several examples.

Note, that I also used the -d flag on split to get numeric extensions because I find those easier to read than the default alphabetic ones. Here's what that gives us:

$ head parts.*
==> parts.00 <==
1
4
7

==> parts.01 <==
2
5
8

==> parts.02 <==
3
6
9

Normally, you would use cat to join files back together that you previously split, but in this case if we want to preserve the original line ordering we can use paste instead:

$ paste -d "\n" parts.*
1
2
3
4
5
6
7
8
9