-1

I have 3 files:

In file1 I've data like this

A,B,C,D,E,F,G
1,2,3,4,5,6,7
7,6,5,4,3,2,1

In file2 I've data like this

A,B,C,D,E,F,G
1,2,3,4,5,5,7
7,6,6,4,3,2,1

In file3 I've only one field i.e A:

A
1
2
7

Now I want to compare file 1 & file2 on column A which is available in file3.

Columns header & number of columns will be same in both files.

Desired output

Item  FILE1   FILE2
1     F=6     F=5
7     C=5     C=6

If there are more columns mismatch

   A,B,C,D,E,F,G
   1,2,3,4,5,5,7
   7,6,5,2,3,2,1

   A,B,C,D,E,F,G
   1,2,3,1,4,6,7
   7,6,6,4,3,2,1

Then output should like this

Item    FILE1   FILE2
1       D=4     D=1
        E=5     E=4
        F=5     F=6
7       C=5     C=6
        D=2     D=4

This is sample, Number of column may vary but it always same in both file1 & file2. And due to this its different from previous question.

0
#!/bin/bash

join -t, file_1 file_2 | join -t, - file_3 |
awk -F, '
NR == 1 {
    fields_num = (NF + 1) / 2

    for(i = 1; i <= fields_num; i++)
        field_names[i] = $i
}
NR != 1 {
    item = $1 "\t"

    for(i = 2; i <= fields_num; i++) {
        cmp_num = i + fields_num - 1

        if($i != $cmp_num) {
            printf "%s%s=%s\t%s=%s\n",
                    item,
                    field_names[i], $i,
                    field_names[i], $cmp_num

            item = "\t"
        }

    }
}'

Few assumptions were made:

  1. The column for comparing always the first.
  2. All files sorted by the first column (A field).
  3. Only those items, that occurs in both files (file_1 andfile_2) are compared. So, if the file_1 has the item 2, but the file_2 hasn't, this line excluded from the comparing.

Input

tail -n +1 -- file_*

==> file_1 <==
A,B,C,D,E,F,G
1,2,3,4,5,5,7
7,6,5,2,3,2,1

==> file_2 <==
A,B,C,D,E,F,G
1,2,3,1,4,6,7
7,6,6,4,3,2,1

==> file_3 <==
A
1
2
7

Output

1   D=4 D=1
    E=5 E=4
    F=5 F=6
7   C=5 C=6
    D=2 D=4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.