paste
command
How to print files side by side.
Adapted from https://www.baeldung.com/linux/merge-files-line-by-line,
which has a nice AWK solution too.
Here is file1:
Code: Select all
# cat /tmp/a
1111
2222
3333
4444
666666
and file2:
Code: Select all
# cat /tmp/b
aaaa
bbbb
cccc
dddd
eeeeeeee
ffff
Notice they don't have matching lengths, and have some empty lines.
Let's use paste
to create a nicely arranged, TAB-delmited set of columns like:
Code: Select all
file1_line1 file2_line1
file1_line2 file2_line2
file1_line3 file2_line3
file1_line4 file2_line4
We'll use column
to help us arrange the the output correctly.
The -t
option tells column
to align things nicely in a table format.
The -n
option tells column
not to merge multiple empty separators (\t
),
which will keep eveything nicely in its columns, accounting for the empty lines.
Code: Select all
# paste /tmp/a /tmp/b | column -t -n
1111 aaaa
2222 bbbb
3333 cccc
4444 dddd
eeeeeeee
666666 ffff
Note, you can change the delimeter that paste
uses with -d
(and column too, with -s
):
Code: Select all
# paste -d'|' /tmp/a /tmp/b | column -t -n -s'|'
1111 aaaa
2222 bbbb
3333 cccc
4444 dddd
eeeeeeee
666666 ffff
To arrange your data a totally different way, you can make paste
put
everything from file1 along the first line of your output file, and
everything from file2 on the second line:
Code: Select all
file1_line1 file1_line2 file1_line3 ...
file2_line1 file2_line2 file2_line3 ...
You achieve this with the -s|--serial
option:
Code: Select all
# paste -d '|' -s /tmp/a /tmp/b
1111|2222|3333|4444||666666
aaaa|bbbb|cccc|dddd|eeeeeeee|ffff