Merging multiple files as columns

There are different cases when we need to concatenate files by their columns. We may need each file's content to appear in separate columns. Usually, the cat command concatenates in a line (or row-wise) fashion.

How to do it...

paste is the command that can be used for column-wise concatenation. The paste command can be used with the following syntax:

$ paste file1 file2 file3 …

Let's try an example as follows:

$ cat file1.txt
1
2
3
4
5
$ cat file2.txt
slynux
gnu
bash
hack
$ paste file1.txt file2.txt
1slynux
2gnu
3bash
4hack
5

The default delimiter is tab. We can also explicitly specify the delimiter by using -d. For example:

$ paste file1.txt file2.txt -d ","
1,slynux
2,gnu
3,bash
4,hack
5,

See also

  • The Column-wise ...

Get Linux Shell Scripting Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.