shell - compare two .csv files using awk\ unix -
file1:
sa, 5006, 12, , dj cn, bn, , bbb, 13 22, 67, gg, ff, 88 33, bb, aa, cc, 22 file2:
sa, 5006, 12, 15 , dj cn, bn, , bbb, 13 empty line 33, cc, aa, dd, 22 output:
sa, 5006, 12, 15 , dj, unmatch, 4 cn, bn, , bbb, 13, match empt, empt, empt, empt, empt, unmatch, 12345 33, cc, aa, dd, 22, unmatch, 24 i need compare 2 .csv files line line, of field/lines can empty , output should in file3: 5 columns form file 2, match\unmatch, unmatch fields this:
c1, c2, c3, c4, c5, match/unmatch, concatenation of digits representing unmatch fields. i try new awk can help? :)
code use, think problem empty fields anf dont know how can print :
##set input , output field separators ':'. begin { fs = ofs = ":" } nr == fnr { ## save line in array, lines saved like: ## c1::c2::c3::c4::c5 ++a[$0] ## process next line beginning. next } ## every line of second file. { ## search line in array, if not exists means field different ## print line. if ( !a[$0] ) { $6 = "same" print }else { $6 = " not same" print } }
you need use line number index of array save between files, can compare corresponding lines in 2 files.
begin { fs = ", "; } nr == fnr { a[fnr] = $0 } # in first file, save each line in array nr != fnr { if (a[fnr] == $0) { # compare line in 2nd file corresponding line in first file $6 = "match"; } else { $6 = "unmatch"; split(a[fnr], b); # split fields first file $7 = "" (i = 1; <= 5; i++) { # compare each field if ($i != b[i]) { $7 = $7 i; } # add non-matching field numbers output } } print; }
Comments
Post a Comment