Using join
command in linux, I would like to compare first column of file1.csv
and file2.csv
and where the column matches, I would like to add a column of file2.csv
to file1.csv
. The first column in file1.csv
and file2.csv
differs and file1.csv
is a subset of file2.csv
(first column wise).
head file1.csv
column1, column2, column3
1.0, 5, 3
1.5, 4, 9
2.1, 2, 1
and
head file2.csv
column1, column2, column4
1.0, 5, 9
1.2, 0, 0
1.3, 0, 1
1.5, 4, 3
2.1, 2, 5
I want to join
such that afterwards:
head file1.csv
column1, column2, column3, column4
1.0, 5, 3, 9
1.5, 4, 9, 3
2.1, 2, 1, 5
I tried join -1 1 -2 1 -t, -o 1.1,1.2,1.3,2.3 <(sort -k 1 file1.csv ) <(sort -k 1 file2.csv )
but it's not working. Because it's also brings the header line bottom:
join -1 1 -2 1 -t, -o 1.1,1.2,1.3,2.3 <(sort -k 1 file1.csv ) <(sort -k 1 file2.csv )
1.0, 5, 3, 9
1.5, 4, 9, 3
2.1, 2, 1, 5
column1, column2, column3, column4
You just concerned about the head columns placed position?
Try it following one. It just modified your command line a little.
join -1 1 -2 1 -t, -o 1.1,1.2,1.3,2.3 <(head -n 1 file1.csv && tail -n+2 file1.csv | sort -k1 ) <(head -n 1 file2.csv && tail -n+2 file2.csv | sort -k1 )
I hope this helps you.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments