Reputation: 141
I have two csv files.
Columns of the first file
col1, col2, col3,......col4095, col4096, col4097
Columns of the second file
col1, col2, col3,......,col4095, col4096, col4097
Expected output - the last column of the first file is discarded
col1, col2, col3.......,col4095. col4096, col1, col2, col3....col4095, col4096, col4097
Both files have the same number of rows, I want to merge the two files into one file.
Upvotes: 2
Views: 15422
Reputation: 42147
With only awk
:
awk -F ',[[:blank:]]*' 'NR==FNR {for (i=1;i<NF;i++) out=out$i", " ; next} \
{out=out$0} END{print out}' f1.csv f2.csv
-F ',[[:blank:]]*'
sets the field separator as ,
followed by any number of space/tab
NR==FNR
will be true for the first file only, then {for (i=1;i<NF;i++) out=out$i", " ; next}
will be executed, which will iterate over, and concatenate all the fields but the last one in variable out
The record of the second file would be concatenated to the variable out
-- {out=out$0}
Finally, the value of variable out
is printed -- END{print out}
Example:
% cat f1.csv
col1, col2, col3,......col4095, col4096, col4097
% cat f2.csv
col1, col2, col3,......,col4095, col4096, col4097
% awk -F ',[[:blank:]]*' 'NR==FNR {for (i=1;i<NF;i++) out=out$i", " ; next} {out=out$0} END{print out}' f1.csv f2.csv
col1, col2, col3, ......col4095, col4096, col1, col2, col3,......,col4095, col4096, col4097
Upvotes: 0
Reputation: 14979
Use paste
:
paste -d, f1.csv f2.csv > out.csv
To ignore last column of first file:
awk -F, 'NF-=1' OFS=, f1.csv | paste -d, - f2.csv > out.csv
Upvotes: 13