Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison.

Thanks in advance for your help.

File A

A,B,C,45,46
B,C,D,55,54
G,H,I,60,61
J,K,L,44,45

File B

A,B,C,47,48
B,C,D,95,99
G,H,I,77,78
File C ( Output File )

A,B,C,47,48
B,C,D,95,99
G,H,I,77,78
J,K,L,44,45

Assuming you want to run this on a Solaris system (as in your earlier posts), try:

/usr/xpg4/bin/awk -F, '
FNR == NR {
        f[$1,$2,$3] = $0
        next
}
{       if(($1,$2,$3) in f)
                print f[$1,$2,$3]
        else    print
}' fileB fileA > fileC
1 Like

Hello,

A littile different approach.

awk 'NR==FNR{a[$1","$2","$3]=$0;next} ($1","$2","$3 in a) {print a[$1","$2","$3]} !($1","$2","$3 in a) {print}' FS=, OFS=, check_file2_check1211 check_file1_check1211

Output will be as follows.

A,B,C,47,48
B,C,D,95,99
G,H,I,77,78
J,K,L,44,45

Where files check_file2_check1211 and check_file1_check1211 are as follows.

cat check_file2_check1211
A,B,C,47,48
B,C,D,95,99
G,H,I,77,78

cat check_file1_check1211
A,B,C,45,46
B,C,D,55,54
G,H,I,60,61
J,K,L,44,45

Thanks,
R. Singh