Comparing fields of two files and displaying results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing fields of two files and displaying results
# 1  
Old 10-14-2015
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

Code:
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

Code:
P036_OMJ_Archivedel
CDRE_MJK_Archivedel
L670_HYV_Archivedel

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

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

Could someone please help.

Thanks
Rahul

Last edited by rahul2662; 10-14-2015 at 08:51 AM..
# 2  
Old 10-14-2015
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.
Code:
 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.
Code:
 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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-14-2015
How about
Code:
awk '
NR==FNR         {_1[$1]++
                 next
                }

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

' exclusionfile.txt masterfile.txt

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing 2 files using awk , not getting any results - C shell

I am using c shell and trying to compare 2 files using awk . But the below awk statement doesnt give any result. Pls. advise why am not getting the desired o/p with the corrected awk script. Need to acheive this solution in awk using C shell. awk 'FNR==NR{a++;next} {for(i in a) {if ( a=$0... (8 Replies)
Discussion started by: reach2khan
8 Replies

2. Shell Programming and Scripting

Comparing two files by two matching fields

Long time listener first time poster. Hope someone can advise. I have two files, 1000+ lines in each, two fields in each file. After performing a sort, what is the best way to find exact matches where field $1 and $2 in file1 are also present in file2 on the same line, then output only those... (6 Replies)
Discussion started by: bstaff
6 Replies

3. Shell Programming and Scripting

Comparing two files using four fields

Dear All, I want to compare File1 and File2 (Separated by spaces) using four fields (Column 1,2,4,5). Logic: If column 1 and 2 of File1 and File2 match exactly and if the File2 has the same characters as any of the characters present in column 4 and 5 of file1 then those lines of file1 and file2... (6 Replies)
Discussion started by: NamS
6 Replies

4. Shell Programming and Scripting

Comparing two files using four fields

I want to compare File1 and File2 (Separated by spaces) using four fields (Column 1,2,4,5). Logic: If column 1 and 2 of File1 and File2 match exactly and if the File2 has the same characters as any of the characters present in column 4 and 5 of file1 then those lines of file1 and file2 are... (1 Reply)
Discussion started by: NamS
1 Replies

5. Shell Programming and Scripting

comparing two files for matching fields

I am newbie to unix and would please like some help to solve the task below I have two files, file_a.text and file_b.text that I want to evaluate. file_a.text 1698.74 1711.88 6576.25 899.41 3205.63 4187.98 697.35 1551.83 ... (3 Replies)
Discussion started by: gameli
3 Replies

6. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

7. Shell Programming and Scripting

Perl: Comparing to two files and displaying the differences

Hi, I'm new to perl and i have to write a perl script that will compare to log/txt files and display the differences. Unfortunately I'm not allowed to use any complied binaries or applications like diff or comm. So far i've across a code like this: use strict; use warnings; my $list1;... (2 Replies)
Discussion started by: dont_be_hasty
2 Replies

8. Shell Programming and Scripting

Comparing two files and inserting new fields

Hi all, I searched the forum and tried to learn from the similar posts. However, I am new and I need to get help on this. I hope an expert kindly help me to sort this out. I need to compare field 1 and 2 of the first file with the same fields of the second file and if both fields matches... (9 Replies)
Discussion started by: GoldenFire
9 Replies

9. Shell Programming and Scripting

Comparing fields in two files

Hi, i want to compare two files by one field say $3 in file1 needs to compare with $2 in file2. sample file1 - reqd_charge_code 2263881188,24570896,439 2263881964,24339077,439 2263883220,22619162,228 2263884224,24631840,442 2263884246,22612161,442 sample file2 - rg_j ... (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

10. Shell Programming and Scripting

Merging two files by comparing three fields

Hi Experts, I need your timely help. I have a problem with merging two files. Here my situation : Here I have to compare first three fields from FILE1 with FILE2. If they are equal, I have to append the remaining values from FILE2 with FILE1 to create the output. FILE1: Class ... (3 Replies)
Discussion started by: Hunter85
3 Replies
Login or Register to Ask a Question