Combine multiple CSV into one single CSV column wi

2019-09-01 05:40发布

问题:

I have many csv files with column 1 with the same information but column 2 is different.

For ex. CSV1 has the following information

AAA, 11
BBB, 22
CCC, 33

And CSV2 has the following

AAA, 1111
BBB, 2222
CCC, 3333

I tried to CAT the files and I end up getting a file concatenated by Rows. But I am looking for something like the following output in a new csv. I am looking for a way to do it in shell.

Result.csv should be

AAA, 11, 1111
BBB, 22, 2222
CCC, 33, 3333

回答1:

Use join:

First file:

$ cat 1
AAA, 11
BBB, 22
CCC, 33

Second file:

$ cat 2
AAA, 1111
BBB, 2222
CCC, 3333

Join them:

$ join -t, 1 2
AAA, 11, 1111
BBB, 22, 2222
CCC, 33, 3333


回答2:

try this one-liner:

 awk -F, -v OFS="," '{a[$1]=a[$1]?a[$1]FS$2:$2}END{for(x in a)print x,a[x]}' file1 2 3 .... 


标签: shell csv