I would like to merge specific columns from two txt files containing varying number of rows, but same number of columns (as shown below):
file1:
xyz desc1 12
uvw desc2 55
pqr desc3 12
file2:
xyz desc1 56
uvw desc2 88
Preferred output:
xyz desc1 12 56
uvw desc2 55 88
pqr desc3 12 0
Currently I use the paste command using awk as:
paste <(awk '{print $1}' file1) <(awk '{print $2}' file1) <(awk '{print $3}' file1) <(awk '{print $3}' file2)
But this seems to merge only columns that overlap. Is there a way in awk to insert zeros instead of omitting the row itself?
I need to combine 100 files together such that my output file will contain 102 columns.
Best Answer
If column-order is important, i.e. numbers from the same file should be kept in the same column, you need to add padding while reading the different files. Here is one way that works with GNU awk:
merge.awk
Here are some test files:
f1
,f2
,f3
andf4
:Test 1
Output:
Test 2
Output:
Edit:
If the output should be tab-separated, set the output field separator accordingly: