Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find difference in csv record

Tags:

shell

diff

csv

perl

Is there an algorithm or diff-like utilities to find difference between two csv files? Example:

file1
-------
key1,value1
key2,value2
key3,value3
key5,value5
key7,value7

file2
-------
key1,value1
key3,value3
key4,value4
key5,value5
key6,value6

With this diff-like utilities it will output 3 types of records:

  1. Records that only exists in file1 (file1 minus file2 set operation)
  2. Records that only exists in file2 (file2 minus file1 set operation)
  3. Records that exists both in file1 and file2 (intersect set operation)
like image 618
hendrasaputra Avatar asked Jan 30 '26 00:01

hendrasaputra


1 Answers

diff can do what you want..

diff file1.csv file2.csv --old-line-format="< %L" --new-line-format="> %L" --unchanged-line-format="= %L"
like image 147
Anon Avatar answered Jan 31 '26 18:01

Anon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!