Linux - join 2 CSV files

后端 未结 4 968
野趣味
野趣味 2020-12-17 18:36

I have 2 CSV files:

file_1 columns: id,user_id,message_id,rate
file_2 columns: id,type,timestamp

The relation between the files is that

4条回答
  •  星月不相逢
    2020-12-17 18:44

    With awk you can try something like this -

    awk -F, 'NR==FNR{a[$3]=$0;next} ($1 in a){print a[$1]","$3 > "file_3"}' file_1 file_2
    

    Test:

    [jaypal:~/Temp] cat file_1     # Contents of File_1
    id,user_id,message_id,rate
    1,3334,424,44
    
    [jaypal:~/Temp] cat file_2     # Contents of File_2
    id,type,timestamp
    424,rr,22222
    
    [jaypal:~/Temp] awk -F, 'NR==FNR{a[$3]=$0;next} ($1 in a){print a[$1]","$3 > "file_3"}' file_1 file_2
    
    [jaypal:~/Temp] cat file_3     # Contents of File_3 made by the script
    1,3334,424,44,22222
    

提交回复
热议问题