Need help in comparing two files in UNIX with a mismatch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in comparing two files in UNIX with a mismatch
# 8  
Old 01-27-2015
Since you are comparing line, you can also try like this

Code:
[akshay@localhost tmp]$ cat f1
1,10
1,20
2,10
2,20
2,30
3,40
4,50

Code:
[akshay@localhost tmp]$ cat f2
1,10
1,20
2,10
2,20
2,30
4,50

Code:
[akshay@localhost tmp]$ nawk  'FNR==NR{X[$0];next}$0 in X{print $0,"- In both";delete X[$0]}END{for(i in X)print i ,"- Not In",FILENAME}' f1 f2
1,10 - In both
1,20 - In both
2,10 - In both
2,20 - In both
2,30 - In both
4,50 - In both
3,40 - Not In f2

# 9  
Old 01-30-2015
Hi,

Could you please help me out with this requirement

Code:
awk -F, 'FNR==NR{X[$1 FS $2]=$1 FS $2;next} ($0 in X){print X[$0] " entry from file1 is present in file2.";delete X[$0]} END{for(i in X){print X[i] " entry NOT present in file2."}}' file1.csv file2.csv

i need output like

Code:
file1.csv
1,10
1,20
2,10
2,20
2,30
3,40
4,50
 
file2.csv
1,10
1,20
2,10
2,20
2,30
4,50
 
o/p 
1,10,1,10 
1,20,1,20
2,10,2,10
2,20,2,20
2,30,2,30
4,50,4,50
3,40,

So basically , it should print both file1 and file2 values and
null when not matching.

The above mentioned command prints values of file1 only.

what changes should be done in the command....can i use an awk inside nawk?

---------- Post updated at 04:43 AM ---------- Previous update was at 04:12 AM ----------

Sorry guys,

that was a silly question Smilie

got it ..

Code:
nawk  'FNR==NR{X[$0];next}$0 in X{print $0,",",$1;delete X[$0]}END{for(i in X)print i}' file1.csv file2.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Compare two files and show the mismatch columns

I need to compare two files and find the mismatch columns in it for csv and fixed width file. Eg: file1 c1,c2,c3,c4<----columnname 1,a,4,d 2,b,5,e 3,c,6,f file2 c1,c2,c3,c4<----columnname 3,x,7,f 2,y,8,e 1,z,9,d output c2,c3<---- mismatch columname a,4 x,7 b,5 or y,8 Ok with... (3 Replies)
Discussion started by: sabzR
3 Replies

2. UNIX for Dummies Questions & Answers

Comparing 2 UNIX files

Hi, I am planning to automate the comparison of data between few tables in 2 different databases ( Teradata and sql server). Below is the approach which I think of. Please suggest any improvements/Modification : 1) The sql server file is having more records and I have to eliminate the... (5 Replies)
Discussion started by: Rahul Raj
5 Replies

3. Shell Programming and Scripting

Mismatch in summing a column in UNIX

Hello, I am facing issue in summing up a column in unix.I am displaying a column sum up to 4 decimal places and below is the code snippet sed '1d' abc.csv | cut -d',' -f7 | awk '{s+=$1}END{ printf("%.4f\n",s)}' -170552450514.8603 example of data values in the column(not... (3 Replies)
Discussion started by: karthik adiga
3 Replies

4. Shell Programming and Scripting

Comparing two files in UNIX and create a new file similar to equi join

I have 2 files namely branch.txt file & RXD.txt file as below Ex:Branch.txt ========================= B1,Branchname1,city,country B2,Branchname2,city,country B3,Branchname3,city,country B4,Branchname4,city,country B5,Branchname5,city,country RXD file : will... (11 Replies)
Discussion started by: satece
11 Replies

5. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: ady_koolz
2 Replies

6. Shell Programming and Scripting

Count mismatch in UNIX

Hi, I have a requirement like below. client is sending the .txt filles.In that file we have 10 records but when I execute the below command it is showing 9 records. klena20> wc -l sample_file.txt|awk '{print $1}' It is showing the output as 9 But in a file records are 10. I found... (7 Replies)
Discussion started by: kirankumar
7 Replies

7. UNIX for Dummies Questions & Answers

Files count mismatch when used with Tar with find

Hi I have used the below steps and found some discrepancies step 1 : find ./ -type f -mtime +7 -name "*.00*" | wc -l = 13519 ( total files ) ( the size of this files is appx : 10GB ) step 2: find ./ -type f -mtime +7 -name "*.00*" | xargs tar zcvf Archieve_7.tar.gz step... (7 Replies)
Discussion started by: rakeshkumar
7 Replies

8. Shell Programming and Scripting

Compare two files and mismatch report

Hi I have two files f1 and f2 and comma separated file. I need to comapre two files by field by field and not by whole line. If they match then skip the line from both the files. If they don't match write the mismatch record from f1 to f3. Assume both the files are sorted on first field. ... (5 Replies)
Discussion started by: dgmm
5 Replies

9. Shell Programming and Scripting

Edited: compare two files and print mismatch

Using unix shell script, how to compare two files and print lines with mismatch? Below are the requirements: 1. The number of lines on the two files is not the same. 2. The difference/mismatch can be found on the second or third column. 3. The comparison is not between line 1 of file 1 and line... (16 Replies)
Discussion started by: kingpeejay
16 Replies

10. Shell Programming and Scripting

comparing two files and find mismatch

hi i have two files and i want to compare both the files and find out mismatch in 3rd file file1 00354|1|0|1|1|0|0|0|1|2 52424|1|0|1|1|0|0|0|1|2 43236|1|0|1|1|0|0|0|1|2 41404|1|0|1|1|0|0|0|1|2 79968|1|0|1|1|0|0|0|1|2 file2 00354|1|0|1|1|0|0|0|1|2 52424|1|0|1|1|0|0|0|0|2... (9 Replies)
Discussion started by: dodasajan
9 Replies
Login or Register to Ask a Question