Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing three .csv files and outputting similarities

Tags:

bash

csv

I am working on a project in bash script to read three different .csv files and output the similar rows. The three .csv files are formated the same way with the same information but the second column may be different. For example, if I have three csv files, named A.csv, B.csv, and C.csv.

A.csv
Animal, Color, Age
Dog, Brown, 9
Cow, White, 3
Cat, Black, 5
Parrot, Blue, 2

B.csv
Animal, Color, Age
Dog, Black, 9
Cow, White, 3
Cat, Brown, 5
Parrot, Blue, 2

C.csv
Animal, Color, Age
Dog, Brown, 9
Cow, White, 3
Cat, Tan, 5
Parrot, Blue, 2

After running the program, I am looking to get an output like:

Animal, Color, Age
Cow, White, 3
Parrot, Blue 2

I have read up on diff3 but that only outputs the differences which is the opposite of what I am trying to do. Any help would be greatly appreciated. Thanks

like image 397
user2525881 Avatar asked Jun 26 '13 21:06

user2525881


1 Answers

Using grep:

grep A.csv -f B.csv | grep -f C.csv

grep -f FILE gets the patterns from the FILE.

Output:

Animal, Color, Age
Cow, White, 3
Parrot, Blue, 2
like image 138
perreal Avatar answered Oct 21 '22 08:10

perreal