views:

282

answers:

3

I want to write a shell script to compare two .csv files. First one contains filename,path the second .csv file contains filename,paht,target. Now, I want to compare the two .csv files and output the target name where the file from the first .csv exists in the second .csv file.

Ex.

a.csv

build.xml,/home/build/NUOP/project1  
eesX.java,/home/build/adm/acl

b.csv

build.xml,/home/build/NUOP/project1,M1
eesX.java,/home/build/adm/acl,M2
ddexse3.htm,/home/class/adm/33eFg

I want the output to be something like this.

M1 and M2

Please help Thanks,

A: 

If you don't necessarily need a shell script, you can easily do it in Python like this:

import csv

seen = set()

for row in csv.reader(open('a.csv')):
  seen.add(tuple(row))

for row in csv.reader(open('b.csv')):
  if tuple(row[:2]) in seen:
    print row[2]
Max Shawabkeh
"don't necessary need a shell script". you make it sound like only Python can do the job.
ghostdog74
Not at all, but I find it simpler this way.
Max Shawabkeh
A: 

if those M1 and M2 are always at field 3 and 5, you can try this

awk -F"," 'FNR==NR{
    split($3,b," ")
    split($5,c," ")
    a[$1]=b[1]" "c[1]
    next
}
($1 in a){
    print "found: " $1" "a[$1]
}' file2.txt file1.txt

output

# cat file2.txt
build.xml,/home/build/NUOP/project1,M1 eesX.java,/home/build/adm/acl,M2 ddexse3.htm,/home/class/adm/33eFg
filename, blah,M1 blah, blah, M2 blah , end

$ cat file1.txt
build.xml,/home/build/NUOP/project1 eesX.java,/home/build/adm/acl

$ ./shell.sh
found: build.xml M1 M2
ghostdog74
The formatting of the question was off. Take another look.
Dennis Williamson
thanks. If i get OP's requirement correct, the output is still the same.
ghostdog74
A: 

try http://sourceforge.net/projects/csvdiff/

Quote: csvdiff is a Perl script to diff/compare two csv files with the possibility to select the separator. Differences will be shown like: "Column XYZ in record 999" is different. After this, the actual and the expected result for this column will be shown.

max muster