How to concatenate files containing columns of data in Linux

Ever wondered how to concatenate text files containing columns next to each other ?

One little bash trick which can help you do this the easy way:

Assume you have for example three files containing something like this:

file1.txt

a    b    c

a    b    c

a    b    c

a    b    c

 

file2.txt

1    2    3

1    2    3

1    2    3

1    2    3

file3.txt

m    n    p

m    n    p

m    n    p

m    n    p

What we want to achieve is the following result:

a    b    c    1    2    3    m    n    p

a    b    c    1    2    3    m    n    p

a    b    c    1    2    3    m    n    p

a    b    c    1    2    3    m    n    p

You have two options

Option 1: Try to do it in bash and die in pain. This is usually not funny.

Option 2: The easy way, use the command "paste":

[user@host ~]$ paste file1.txt file2.txt file3.txt
a    b    c     1    2    3     m    n    p

a    b    c     1    2    3     m    n    p

a    b    c     1    2    3     m    n    p

a    b    c     1    2    3     m    n    p

 

The command paste will concatenate the files for you

The program have some cool options, like you can set separator, or use the -s option which will cause the following behavior "paste one file at a time instead of in parallel"

Here is an example with the same files:


[user@host ~]$ paste -s file1.txt file2.txt file3.txt

a    b    c             a    b    c             a    b    c             a    b    c

1    2    3            1    2    3             1    2    3             1    2    3

m    n    p          m    n    p            m    n    p            m    n    p

Each row, becomes a new column.

Have fun :)