CLI tip 31: concatenate files column wise
The paste
command is typically used to merge two or more files column wise. By default, paste
adds a tab character between corresponding lines of input files.
$ cat colors_1.txt
Blue
Brown
Orange
Purple
$ cat colors_2.txt
Black
Blue
Green
Orange
$ paste colors_1.txt colors_2.txt
Blue Black
Brown Blue
Orange Green
Purple Orange
You can use the -d
option to change the delimiter between the columns. The separator is added even if the data has been exhausted for some of the input files.
$ paste -d'|' <(seq 3) <(seq 4 5) <(seq 6 8)
1|4|6
2|5|7
3||8
# note that the space between -d and empty string is necessary here
$ paste -d '' <(seq 3) <(seq 6 8)
16
27
38
# use newline separator to interleave file contents
$ paste -d'\n' <(seq 11 12) <(seq 101 102)
11
101
12
102
You can use empty files to get multicharacter separation between the columns. The pr
command is better suited for this task.
$ paste -d' : ' <(seq 3) /dev/null /dev/null <(seq 4 6)
1 : 4
2 : 5
3 : 6
$ pr -mts' : ' <(seq 3) <(seq 4 6)
1 : 4
2 : 5
3 : 6
Video demo:
See paste command chapter from my Command line text processing with GNU Coreutils ebook for more details.