Comparing fields of two files and displaying results

Hello ,

I am trying to compare two files i.e one master file and the other exclusion file. If the second field of masterfile is oracle8 then I need to compare the 3rd field of master file with the 1st field of all the rows of exclusion file else I need to compare 2nd field from master file with 1st field of all the rows of the exclusion list. I want to list only those records from master file whose 2nd/3rd field (3rd field if oracle8 is 2nd field else 2nd field) does not match the 1st field of any of the rows of exclusion file

For example in the below case Output should be 1st and 3rd line from master File because in 1st row , 2nd field of master file is not there in the exclusion list and in 3rd row , 3rd field is not there in the exclusion list :

Master File

Backup  p00crftifdf01_App_Weekly Failed          full    10/12/2015 12:00:04 AM  1444579204      10/12/2015 02:28:42 AM  1444588122      0:00    2:28    247.33  1      00       0       0       0       1       1       189950  100%    root.pwed@p342dfrtc01  2015/10/12-3
Backup  Oracle8 P036_OMJ_Archivedel     Completed       full    10/12/2015 01:00:42 AM  1444582842      10/12/2015 01:08:56 AM  1444583336      0:00    0:08    0.18   10       0       0       0       0       5       5       0       100%    oracle.all@p342-backup-crf.out.peht.se   2015/10/12-30
Backup  Oracle8 p043_ERB_archivedel     Completed       full    10/12/2015 01:15:01 AM  1444583701      10/12/2015 01:17:53 AM  1444583873      0:00    0:02    0.91   10       0       0       0       0       2       2       0       100%    oracle.all@p102-backup-crf.out.peht.se   2015/10/12-31

Exclusion File

P036_OMJ_Archivedel
CDRE_MJK_Archivedel
L670_HYV_Archivedel

I tried following but not able to include the logic for oracle8 as explained above

awk -F' ' 'NR==FNR{_1[$1]++;next}!_1[$3]' exclusionfile.txt masterfile.txt

Could someone please help.

Thanks
Rahul

Hello rahul2662,

Thank you for updating the sample input and expected output in your post. Could you please try following and let me know if this helps.

 awk 'FNR==NR{if($2=="Oracle8"){A[$3]=$0} else {A[$2]=$0};next} ($0 in A){print A[$0]}' master_file Exclusion_file
 

Output will be as follows.

 Backup  Oracle8 P036_OMJ_Archivedel     Completed       full    10/12/2015 01:00:42 AM  1444582842      10/12/2015 01:08:56 AM  1444583336      0:00    0:08    0.18   10       0       0       0       0       5       5       0       100%    oracle.oinstall@p114pldbms01-backup-vip.intra.nehr.sg   2015/10/12-30
 

Hope this helps.

Thanks,
R. Singh

1 Like

How about

awk '
NR==FNR         {_1[$1]++
                 next
                }

                {IX = 2 + ($2 == "Oracle8")
                }
!_1[$IX]

' exclusionfile.txt masterfile.txt
1 Like