Comparing 2 files using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing 2 files using shell script
# 1  
Old 06-01-2012
Comparing 2 files using shell script

Hi Experts,

I have 2 files 1 file consists of 800 records and 2 file consists of 100 records with matching column as Membership_Num.So i need a script which will compare the 2 files and displays the output.As these are the files the script should take any delimter like (tab,comma) as input while comparing.In the script logic to be fulfilled should be like

File1 File2
1 1
2 3
3 5
4 2

Assuming these numbers as membership_num it should compare correctly.Please help me out for this.

Regards,
Naveen
# 2  
Old 06-01-2012
what is your input examples and desired output ?
# 3  
Old 06-01-2012
Hi,
Thks for the quick response,
Input file is like
Membership Nm 104752100
61883330
976096810
100015391
100015391
100015391
100015391
262166481
266713211
266713211
322316641
322316641
322316641
322316641
322493281
322493281 which contains duplicate records also.There will be another file with some of the same membership records.It should compare and display the count as output.

Last edited by naveen.dasu; 06-01-2012 at 09:17 AM..
# 4  
Old 06-01-2012
your input file ?

Code:
Membership Nm 104752100
61883330
976096810
100015391
100015391
100015391
100015391
262166481
266713211
266713211
322316641
322316641
322316641
322316641
322493281
322493281

or

Code:
Membership Nm 
104752100
61883330
976096810
100015391
100015391
100015391
100015391
262166481
266713211
266713211
322316641
322316641
322316641
322316641
322493281
322493281

# 5  
Old 06-01-2012
The input is second one which u posted.
# 6  
Old 06-01-2012
Code:
# cat file1
Membership Nm
104752100
61883330
976096810
100015391
100015391
100015391
100015391
262166481
266713211
266713211
322316641
322316641
322316641
322316641
322493281
322493281
32249328155555555

# cat file2
Membership Nm
104752100
61883330
976096810
100015391
100015391
100015391
100015391
262166481
266713211
266713211
322316641
322316641
322316641
322316641
322493281
322493281
322493281
3224932811111111
3224932222222222
3224932333333333
3224932333333334

Code:
# awk 'NR==FNR&&NR!=1{a[$1]++;f=FILENAME;next}{if(FNR!=1)b[$1]++}
END{
printf "%-10s%s%15s\n",f,FILENAME,"number";for(j in b)for(i in a)if(i==j)printf "%-10s%s%22s%32s\n",a[i],b[j],i,"<<SHARED RECORD>>";
else {if((i in a)&&!(i in b)&&(pp!~i)){printf "%-10s%31s\n",a[i],i;pp=i}if((j in b)&&!(j in a)&&(p!~j)){printf "%11s%29s\n",b[j],j;p=j}}
}' file1 file2
file1     file2         number
2         2             266713211               <<SHARED RECORD>>
1                       32249328155555555
          1             3224932222222222
2         3             322493281               <<SHARED RECORD>>
4         4             322316641               <<SHARED RECORD>>
4         4             100015391               <<SHARED RECORD>>
1         1              61883330               <<SHARED RECORD>>
1         1             262166481               <<SHARED RECORD>>
1         1             976096810               <<SHARED RECORD>>
          1             3224932333333333
1         1             104752100               <<SHARED RECORD>>
          1             3224932333333334
          1             3224932811111111

regards
ygemici

Last edited by ygemici; 06-02-2012 at 10:02 AM.. Reason: printf modify
# 7  
Old 06-02-2012
Thank you very much ygemici..Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. UNIX for Dummies Questions & Answers

Shell script needed for comparing two files

Hi, I need shell script to compare the two files based on certain fields and output should contains the required fields based on result.pls find sample input files and required output file 1 COUNT, BNG_IP,PORT,OVLAN 22 , 10.238.60.129,1/1,2009 144 , 10.238.60.129,1/1,2251 ... (5 Replies)
Discussion started by: surender reddy
5 Replies

3. Shell Programming and Scripting

Need Help in comparing 2 text files in shell script

Hi All, I have 2 files like below vi f1 frog elephant rabit zebra dog vi f2 rabit dog ============== Now i want to comapre two files and the result will be frog (8 Replies)
Discussion started by: kumar85shiv
8 Replies

4. Shell Programming and Scripting

Need help in comparing two files using shell or Perl

I have these two file that I am trying to compare using shell arrays. I need to find out the changed or the missing enteries from File2. For example. The line "f nsd1" in file2 is different from file1 and the line "g nsd6" is missing from file2. I dont want to use "for loop" because my files... (2 Replies)
Discussion started by: sags007_99
2 Replies

5. Shell Programming and Scripting

shell script for comparing two files

Hi, I have 2 files as below FILE1.dat co1|co2|co3 abnd|45.56|AZ dkny|30.2|PA sam|.23|VA FILE.CTL FILENAME|FILE1.dat NO OF RECORDS|3 CHECKSUM|75.99 Could you please help me to write a shell script to compare 1. TOTAL RECORDS from FILE1.dat with NO of RECORDS in FILE.CTL 2.... (8 Replies)
Discussion started by: dreamsportsteam
8 Replies

6. Shell Programming and Scripting

comparing 2 files in shell script

I have 2 files config1h.txt ----------------- BFMU=ENABLE,ID=PM THR=OFF,REP=ALL,CON=IACM,TIM=OFF;GPON collection strategy 1.3.6.1.2.1.2.2:8 1.3.6.1.2.1.2.2:7 1.3.6.1.4.1.637.61.1.35.11.4:4 1.3.6.1.4.1.637.61.1.35.11.4:3 1.3.6.1.4.1.637.61.1.35.10.1:2 1.3.6.1.4.1.637.61.1.35.10.1:43... (7 Replies)
Discussion started by: LavanyaP
7 Replies

7. Shell Programming and Scripting

comparing two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file and the files... (2 Replies)
Discussion started by: revenna
2 Replies

8. UNIX for Dummies Questions & Answers

shell script for comparing 2 files

Hi, how to read the 2 files and compare each other in shell script? i have 2 files test1 and test2, both files contains 20 character records.we have to compare file 1 records with file2, if exists then reject the record else we have to append it to test2 file. in file test1 around 100 single... (2 Replies)
Discussion started by: prashanth.spl
2 Replies

9. Shell Programming and Scripting

shell script comparing files in a file

There is a text file that contains the data in the following format: COLUMN1 COLUMN2 ABC 1 ABC 2 ABC 3 DEF 4 DEF 5 XYZ 7 We have to create a second text file... (4 Replies)
Discussion started by: raina_nalin
4 Replies
Login or Register to Ask a Question