简体   繁体   English

使用键列加入两个csv

[英]join two csv using key columns

I would like to join two files using the key column the name of the city. 我想使用关键字列城市名称来加入两个文件。 I want to join only the data of the cities that are repeated in both csv... 我只想加入在两个csv中重复的城市的数据...

For example 例如

File1.csv File1.csv

London, 10,15
Rome, 12,18
Paris, 8, 16
Lissabon, 10,17

File2.csv File2.csv

London, 11,16
Berlin, 13,19
Paris, 12,18
Lissabon, 11,19

Result I wish, 结果我希望,

London,10,15,11,16 
Paris,8,16,12,18
Lissabon,10,17,11,19

How can I do it in bash? 我该怎么做呢?

bash has a join command, though it requires that the input be sorted: bash有一个join命令,尽管它要求对输入进行排序:

$ join -j 1 -t ',' <(sort File1.csv) <(sort File2.csv)
Lissabon, 10,17, 11,19
London, 10,15, 11,16
Paris, 8, 16, 12,18

Using this awk : 使用这个awk

awk -F, 'FNR==NR {a[$1]=$0;next} $1 in a{p=$1; sub(/^[^,]+, */, "");
         print a[p], $0}' OFS=, file1 file2
London, 10,15,11,16
Paris, 8, 16,12,18
Lissabon, 10,17,11,19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM