Print two files in two columns
Solution 1
What about paste file{1,2}| column -s $'\t' -tn
?
looooooooong line line hello
line world
-
This is telling
column
to useTab
as columns' separator where we takes it from thepaste
command which is the default seperator there if not specified; generally:paste -d'X' file{1,2}| column -s $'X' -tn
where
X
means any single character. You need to choose the one which granted that won't be occur in your files. The
-t
option is used to determine the number of columns the input contains.- This will not add long tab between two files while other answers does.
-
this will work even if there was empty line(s) in file1 and it will not print second file in print area of file1, see below input/ouput
Input file1:
looooooooong line line
Input file2:
hello world
Output:
looooooooong line hello world line
Solution 2
With single pr
command:
pr -Tm file[12]
-T
(--omit-pagination
) - omit page headers and trailers, eliminate any pagination by form feeds set in input files-m
(--merge
) - print all files in parallel, one in each column
Solution 3
Try:
paste -d '\n' file1 file2 | xargs -d '\n' printf '%-30s %-30s\n'
Inspired by @Kusalananda's solution.
Note: The -d
parameter of xargs
is only available on GNU version, but not on BSD.
Solution 4
A portable solution:
$ paste file1 file2 | awk -F'\t' '{ printf("%-30s %s\n", $1, $2) }'
looooooooong line hello
line world
This uses paste
to produce a tab-delimited input for awk
.
The awk
script simply takes the two tab-delimited fields and outputs them using printf()
. A column of 30 characters is reserved for the first file. The %-30s
means "30 positions of string data with left alignment". Removing the -
would produce a column aligned to the right, and changing 30
would change the column width.
It also deals with uneven length files. Here I've added lines to the second file:
looooooooong line hello
line world
hello
world
hello
world
And, when reversing the order of the files on the command line:
hello looooooooong line
world line
hello
world
hello
world
Related videos on Youtube
belkka
Updated on September 18, 2022Comments
-
belkka over 1 year
I want to print two files in two columns -- first file on the left side and second on the right side.
paste
doesn't do the job, because it can only insert a character as delimiter, so if first file lines have different length output will be twisted:$ cat file1 looooooooong line line $ cat file2 hello world $ paste file1 file2 looooooooong line hello line world
If it was a command to add trailing spaces like
fmt --add-spaces --width 50
the problem would be solved:$ paste <(fmt --add-spaces --width 50 file1) file2 looooooooong line hello line world
But I don't know a simple way to do this.
So how to merge and print several files horizontally without twisting? Actually, I just want to look at them simultaneously.
UPD: command to add trailing spaces does exist (for example,
xargs -d '\n' printf '%-50s\n'
)But solution like
$ paste <(add-trailing-spaces file1) file2
does not work as expected when file1 has fewer lines than file2.
-
Olaf Dietsche over 6 yearsYou may use
awk
for your hypotheticalfmt
command, see stackoverflow.com/a/9394541/1741542 as an awk
-
-
Kusalananda over 6 yearsWhat does the
-T
do? -
belkka over 6 yearsGenerally
paste -d @ file1 file2 | column -t -n -s @
where@
is any delimiter that is not contained in files. -
belkka over 6 yearsThis can truncate long lines without any warning, but I like it.
-
αғsнιη almost 6 years@belkka exactly
-
paradroid over 4 yearsI had been trying this but without the
$
. This is the answer for me, but could you explain what the$
is doing here? -
αғsнιη over 4 years@paradroid please see gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
-
Ulysse BN over 4 yearsOn my mac I had to use
pr -tm file[12]
. (minus-t
)