Reputation: 131
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
Upvotes: 1
Views: 1803
Reputation: 195059
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 ....
Upvotes: 0
Reputation:
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
Upvotes: 1