Joining one column from a file to another file based on matched value of first column in both

hi15

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
Daein Park

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Awk: replace column in one file with one column from another file

From Dev

Remove lines from a file based on patterns in another file which may partially match a particular column in first file

From Dev

Copy value from one column based on the value of another column

From Dev

Copy value from one column based on the value of another column

From Dev

Split one column of csv file based on another column

From Dev

Joining two file data based on column comparision

From Dev

Copying a column from one CSV file to another

From Dev

get value of one column by another column in csv file python

From Dev

get value of one column by another column in csv file python

From Dev

Compare one column from one file with all columns in another file

From Dev

How to get value from one column of a text file for values of another column

From Dev

Remove lines from file if the first column appears in another file

From Dev

Replace column in one file with column from another using awk?

From Dev

Updating one column based on the value of another column

From Dev

adding values from one column of a data frame into a new column of another dataframe if the first two columns in both match

From Dev

Compare 2 files with awk and substract value from one column if the value of another column matches in both files

From Dev

Paste multiple files based on the first column into one single file

From Dev

Partition a file based on one column

From Dev

Summing values from a column based on match in another column and first distinct occurrence of value in a third column

From Dev

How to seperate rows based on the value of the first column from a csv file in Python

From Dev

Update column value from one position to another position based on indexed

From Dev

move value from one column to another based on condition

From Dev

add a new column to the file based on another file

From Dev

Get Distinct List from csv file based on first column

From Dev

Python - Calculating the second column from the first one in a file

From Dev

Append lines from csv file to another based on column 1 matches

From Dev

Add column from one .csv to another .csv file

From Dev

Add column from one .csv to another .csv file

From Dev

Copying an entire column from one CSV file to another using Powershell

Related Related

  1. 1

    Awk: replace column in one file with one column from another file

  2. 2

    Remove lines from a file based on patterns in another file which may partially match a particular column in first file

  3. 3

    Copy value from one column based on the value of another column

  4. 4

    Copy value from one column based on the value of another column

  5. 5

    Split one column of csv file based on another column

  6. 6

    Joining two file data based on column comparision

  7. 7

    Copying a column from one CSV file to another

  8. 8

    get value of one column by another column in csv file python

  9. 9

    get value of one column by another column in csv file python

  10. 10

    Compare one column from one file with all columns in another file

  11. 11

    How to get value from one column of a text file for values of another column

  12. 12

    Remove lines from file if the first column appears in another file

  13. 13

    Replace column in one file with column from another using awk?

  14. 14

    Updating one column based on the value of another column

  15. 15

    adding values from one column of a data frame into a new column of another dataframe if the first two columns in both match

  16. 16

    Compare 2 files with awk and substract value from one column if the value of another column matches in both files

  17. 17

    Paste multiple files based on the first column into one single file

  18. 18

    Partition a file based on one column

  19. 19

    Summing values from a column based on match in another column and first distinct occurrence of value in a third column

  20. 20

    How to seperate rows based on the value of the first column from a csv file in Python

  21. 21

    Update column value from one position to another position based on indexed

  22. 22

    move value from one column to another based on condition

  23. 23

    add a new column to the file based on another file

  24. 24

    Get Distinct List from csv file based on first column

  25. 25

    Python - Calculating the second column from the first one in a file

  26. 26

    Append lines from csv file to another based on column 1 matches

  27. 27

    Add column from one .csv to another .csv file

  28. 28

    Add column from one .csv to another .csv file

  29. 29

    Copying an entire column from one CSV file to another using Powershell

HotTag

Archive