Comparing files columnwise and print the differences in third file

Hello Everybody!!!!!!!!! Request you to help me with the below mentioned issue:

I have 2 files say,

File 1:
a|4|7
b|3|2
c|8|8
d|8|9

File 2:
a|4|6
b|2|2
c|8|8
d|9|8

The third file(output file) should have:
Data mismatch in row 1 column 3
Data mismatch in row 2 coumn 2
Data mismatch in row 4 column 2 and column 3

Or atleast be able to capture the row and column number having mismatch.....

Please let me know if it is possible

Hello there,

I think the following KornShell script will do the job. Note that this script works if and only if we suppose that in each file there are 4 lines and 3 columns. Otherwise a modification is needed in order to have it working for an arbitrary number of lines and columns.

#!/bin/ksh

INPUT_FILE1=$1
INPUT_FILE2=$2
OUTPUT_FILE=$3

exec 4< $INPUT_FILE1
exec 5< $INPUT_FILE2
exec 6> $OUTPUT_FILE

    
for LINE in 1 2 3 4
do
    read -u4 CURRENT_LINE_IN_FILE1
    read -u5 CURRENT_LINE_IN_FILE2
    
    for COLUMN in 1 2 3
    do
        TOKEN1=$(echo $CURRENT_LINE_IN_FILE1 | cut -d '|' -f $COLUMN)
        TOKEN2=$(echo $CURRENT_LINE_IN_FILE2 | cut -d '|' -f $COLUMN)
        
        if (( (($LINE == 1) && ($COLUMN == 3)) ||
              (($LINE == 2) && ($COLUMN == 2)) ||
              (($LINE == 4) && (($COLUMN == 2) || ($COLUMN == 3))) ))
        then
            if [[ $TOKEN1 != $TOKEN2 ]]
            then
                print -u6 "Mismatch found in (line , column) = "\
                "($LINE , $COLUMN), in file: $INPUT_FILE1 found "\
                "$TOKEN1 but in file: $INPUT_FILE2 found $TOKEN2"
            fi
        fi
    done
done

4<&-
5<&-
6<&-

Regards,
:slight_smile:

Thanks a lot....actually the no. of columns are static(i.e. can be greater than 3) while number of rows are dyanamic.Also I am currently working on csh shell.
Please let me know if it can be done to satisfy the above requirements.
Also let me know if we can do it using any command like diff,cmp,comm or anything else.

Please let me know if it is at all possible.......I have tried almost everything except awk script.But not able to arrive at a solution.Please suggest