Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Urgent help needed with awk to compare 2 csv files

114 views
Skip to first unread message

ashar...@gmail.com

unread,
Sep 12, 2012, 9:54:52 AM9/12/12
to
Hi experts,
I am new to awk so I need help from u all.
I have 2 csv files and I want to compare them using awk and generate a new file.

file1.csv

"no","loc"
"abc121","C:/pro/in"
"abc122","C:/pro/abc"
"abc123","C:/pro/xyz"
"abc124","C:/pro/in"

file2.csv

"no","loc"
"abc121","C:/pro/in"
"abc122","C:/pro/abc"
"abc125","C:/pro/xyz"
"abc126","C:/pro/in"

output.csv

"file1","file2","Diff"
"abc121","abc121","Match"
"abc122","abc122","Match"
"abc123","","Unmatch"
"abc124","","Unmatch"
"","abc125","Unmatch"
"","abc126","Unmatch"



Kindly help ASAP

Ed Morton

unread,
Sep 12, 2012, 11:36:01 AM9/12/12
to
try this (untested):

awk '
BEGIN{ FS=OFS=","; q="\"" }
FNR == 1 { fname=FILENAME; sub(/\.csv/,"",fname); next }
NR == FNR { fname1=fname; f1[$1]; next }
{ fname2=fname; f2[$1] }
END {
print q fname1 q, q fname2 q, q "Diff" q
for (field in f1) {
if (field in f2) {
print field, field, q "Match" q
delete f1[field]
delete f2[field]
}
}
for (field in f1) {
print field, q q, q "Unmatch" q
}
for (field in f2) {
print q q, field, q "Unmatch" q
}
}' file1.csv file2.csv > output.csv

Ed.

0 new messages