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
I want to achieve something like this where i can see the mismatched columns as well along with mismatched rows. I have tried
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.
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:
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:
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?
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: