Comparing two files and writing mismatched rows along with mismatched columns. Pointing out the mism


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Comparing two files and writing mismatched rows along with mismatched columns. Pointing out the mism
# 1  
Old 05-08-2017
Comparing two files and writing mismatched rows along with mismatched columns. Pointing out the mism

I got a requirement where I need to compare two files wrt to each columns and write the corresponding difference in another file along with some identification showing mismatched columns. Pointing out the mismatched columns is my main problem statement. For example we have files like:
File 1
Code:
1|piyush|bangalore|dev 
1|piyush|bangalore|QA 
2|pankaj|bangalore|dev 
3|rohit|delhi|QA

File 2
Code:
 1|piyush|bangalore|QA 
1|piyush|bangalore|QA 
2|pankaj|bangalore|dev 
3|rohit|bangalore|dev

The expected output file looks somewhat like.

Code:
 File 1 
1|piyush|bangalore|**dev**
File 2 
1|piyush|bangalore|**QA**
File 1 
3|rohit|**delhi**|**QA**
File 2 
3|rohit|**bangalore**|**dev**

I want to achieve something like this where i can see the mismatched columns as well along with mismatched rows. I have tried

Code:
diff File1 File2 > Diff_File

But this is giving me only the mismatched records or rows. I am not getting any way to point out the mismatched columns as well. Please help me out if its possible to do is using shell script or awk command as i am very new to this. Thanks in advance.

Last edited by piyush pankaj; 05-08-2017 at 02:04 PM.. Reason: improved formatting
# 2  
Old 05-08-2017
Are the corresponding records identified by line No., or by a key value?
# 3  
Old 05-08-2017
Quote:
Originally Posted by piyush pankaj
But this is giving me only the mismatched records or rows. I am not getting any way to point out the mismatched columns as well. Please help me out if its possible to do is using shell script or awk command as i am very new to this. Thanks in advance.
Before writing any code: notice that you are implicitly working context-oriented and - as a principle - you cannot do this with regexps. You will need a parser! (see here for an in-depth discussion about this). For instance, you break down this:

Code:
3|rohit|**delhi**|**QA**

Into two differences, "delhi/bangalore" and "QA/dev". But without your (outside) knowledge of "delimiters" and "fields", etc. - that is: you involuntarily parsing the input as you read it - there is only one different part. The fact that "|" occurs once in the first variant (from file 1) and once in the second variant (from file 2) means only as much as the "e" in "delhi" occurring in "bangalore" too.

That means, you need to "build into" the script you are about to write this knowledge. You can start by simply reading line for line and splitting it into fields. These fields can then be compared.

Notice that you still need to define how different lines are to be matched. Consider these two file contents:

Code:
a|b|c     a|b|c
d|e|f     d|x|f
d|x|f     d|e|f

will these files be "identical" per your definition because the sequence of the lines doesn't matter or will these be considered two differences because it does?

I hope this helps.

bakunin
# 4  
Old 05-08-2017
Based on the assumption that you identify records by their line No., how about
Code:
awk '
        {getline TL < FN
         n = split (TL, T)
         for (i=1; i<=NF; i++) if ($i != T[i])  {$i   = "**" $i "**"
                                                 T[i] = "**" T[i] "**"
                                                }
         print > (FILENAME ".res")
         for (i=1; i<=n; i++) printf "%s%s", T  [i], i==n?ORS:OFS > (FN ".res")
        }
' FS="|" OFS="|" FN=file1 file2
cf *.res
file1.res:
1|piyush|bangalore|**dev **
1|piyush|bangalore|QA 
2|pankaj|bangalore|dev 
3|rohit|**delhi**|**QA**
file2.res:
 1|piyush|bangalore|**QA **
1|piyush|bangalore|QA 
2|pankaj|bangalore|dev 
3|rohit|**bangalore**|**dev**

# 5  
Old 05-08-2017
Quote:
Originally Posted by RudiC
Are the corresponding records identified by line No., or by a key value?
Hi, no we don't have any line no or key in my case
# 6  
Old 05-08-2017
HI.

Using your sample data, here is a solution using a GNU utility, dwdiff. Basically, it operates on space-separated words, so I modified your sample files to be that instead of | separated:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate field/word diff, dwdiff.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C dwdiff

FILE=${1-data1}

pl " Input data files:"
head data?

pl " Amended data files::"
sed 's/|/ /g' data1 > t1
sed 's/|/ /g' data2 > t2
head t?

pl " Results:"
dwdiff t1 t2

pl " More details for dwdiff:"
dixf dwdiff

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
dwdiff 2.0.9

-----
 Input data files:
==> data1 <==
1|piyush|bangalore|dev 
1|piyush|bangalore|QA 
2|pankaj|bangalore|dev 
3|rohit|delhi|QA

==> data2 <==
1|piyush|bangalore|QA 
1|piyush|bangalore|QA 
2|pankaj|bangalore|dev 
3|rohit|bangalore|dev

-----
 Amended data files::
==> t1 <==
1 piyush bangalore dev 
1 piyush bangalore QA 
2 pankaj bangalore dev 
3 rohit delhi QA

==> t2 <==
1 piyush bangalore QA 
1 piyush bangalore QA 
2 pankaj bangalore dev 
3 rohit bangalore dev

-----
 Results:
1 piyush bangalore [-dev-] {+QA+} 
1 piyush bangalore QA 
2 pankaj bangalore dev 
3 rohit [-delhi QA-] {+bangalore dev+}

-----
 More details for dwdiff:
dwdiff  a delimited word diff program (man)
Path    : /usr/bin/dwdiff
Version : 2.0.9
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help    : probably available with --help
Repo    : Debian 8.7 (jessie)

Note that the braces enclose the markers + for an addition, - for a deletion.

There are a number of additional options. See man dwdiff for details.

Best wishes ... cheers, drl
# 7  
Old 05-08-2017
Quote:
Originally Posted by piyush pankaj
Hi, no we don't have any line no or key in my case
Aaaahh - so why don't you compare 3 rohit delhi QA to 1 piyush bangalore QA?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Mismatched free() / delete / delete [] line no missing

Could you tell me the possibilities of the reason to get the Mismatched free() / delete / delete . I unable to see the line no in the valgrind report. it displays the function name. with that function name, I am not able to find where exactly the issue is there.I am getting the Mismatched free()... (3 Replies)
Discussion started by: SA_Palani
3 Replies

2. Shell Programming and Scripting

awk delimiter count if mismatched?

Hi all, I have a file where in it has lot of records in it. I have written below stuff to find the number of fields as shown below `awk -F '|' '{print NF-1}' file.txt| head -1` how do i proceed if in case any record in particular is having more number of delimiters, if it having??? what... (7 Replies)
Discussion started by: nikhil jain
7 Replies

3. UNIX for Dummies Questions & Answers

Writing a script to take the average of two columns every 3 rows

I have a dataset with 120 columns. I would like to write a script, that takes the average of every two columns, starting from columns 2 and 3, and moving consecutively in frames of 3 columns, all the way until the last column. The first column in the output file would be the averages of columns... (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Comparing Columns and writing a new file

I have a table with one column File1.txt 1 2 3 4 5 6 7 8 9 10 Another table with two columns; This has got a subset of entries from File 1 but not unique because they have differing values in col 2. File2.txt 1 a 2 d 2 f 6 r 6 e (3 Replies)
Discussion started by: cs_novice
3 Replies

5. Shell Programming and Scripting

Comparing rows and columns

Hi, i've a .csv file with the data as below: - file1.h, 2.0 file2.c, 3.1 file1.h, 2.5 file3.c, 3.3.3 file1.h, 1.2.3 I want to remove the duplicate file names considering only the one with the highest version number.. output should be file1.h, 2.5 file2.c, 3.1 file3.c,... (3 Replies)
Discussion started by: pravsripad
3 Replies

6. Shell Programming and Scripting

Comparing rows in same file and writing the result in new file

Help needed... Can you tell me how to compare the last two couple entries in a file and print their result in new file..:confused: I have one file Check1.txt \abc1 12345 \abc2 12327 \abc1 12345 \abc2 12330 I want to compare the entries in Check1 and write to... (1 Reply)
Discussion started by: kichu
1 Replies

7. Linux

Error Mismatched object file

Dear All, Need your help to rectify this error. Recently I have upgraded my Linux server from 32 bit to 64 bit server. OS details are Red Hat Enterprise Linux Server release 5.3 Kernel 2.6.18-120.el5 on an x86_64 After upgradation, when i try to compile or catalog any program, it is... (2 Replies)
Discussion started by: mysmileforu
2 Replies

8. HP-UX

ld: Can't find library or mismatched ABI for -lstlport_aCC

Hi All, while compiling on HP UX, i am getting the following error ld: Can't find library or mismatched ABI for -lstlport_aCC i am new to unix and HPUX, please suggest solution ASAS thanks in advance vindhyalesh (3 Replies)
Discussion started by: vindhyalesh
3 Replies

9. HP-UX

ld: Can't find library or mismatched ABI for

I started building a program which connects to MySQL and got: ld: Can't find library or mismatched ABI for -lmysqlclient_r I tried building my own MySQL and use these, and it worked but then it gave me: ld: Can't find library or mismatched ABI for -lrt But librt is a system library (and... (1 Reply)
Discussion started by: selalerer
1 Replies

10. HP-UX

ld: Can't find library or mismatched ABI for -lstdc++

Hi, I am having problem of linking .o and I am working with HP -UX gcc version is 3.4.2 . I have complied few cpp files and got .o's. But at the time of linking i am having issue. it is returning ld: Can't find library or mismatched ABI for -lstdc++ Fatal error. I have lib at location ... (0 Replies)
Discussion started by: ghananil
0 Replies
Login or Register to Ask a Question