Compare line and printing difference

Hi,

I want to compare two files and print out their differences
e.g:
t1.txt

a,b,c,d

t2.txt

a,b,c,d,e,f

Output

e,f

Currently I do this long about way

tr ',' '\n' <t1.txt >t1.tmp
tr ',' '\n' <t2.txt >t2.tmp
diff t1.tmp t2.tmp > t12.tmp

I have to this comparison for 100 files, so looking for a cleaner way without doing tr and storing in tmp file

Hello Wahi,

Could you please try following and let me know if this helps you.

 awk -F, 'FNR==NR{split($0, A,",");next} {for(i=1;i<=NF;i++){D=$i;for(D in A){delete A}}} END{for(j in A){print A[j]}}' t2 t1
 

output will be as follows.

e
f
 

EDIT: Adding a non one-liner form of solution too.

awk -F, 'FNR==NR        {
                                split($0, A,",");
                                next
                        }
                        {       for(i=1;i<=NF;i++)      {
                                                                D=$i;
                                                                for(D in A)     {
                                                                                        delete A
                                                                                }
                                                        }
                        }
         END            {       for(j in A)             {
                                                                print A[j]
                                                        }
                        }
         ' t2 t1
 

Thanks,
R. Singh

Hi Ravinder,

Your solution almost got what I wanted. The only thing is that the code needs to check if the order is same too. I think I missed adding that in my original thread
e.g: t1.txt

a,b

t2.txt

b,a

It should tell that files are different and why

Maybe I should do a cksum first and then if different, check if it is different because of order, different because field name is different or extra field.

So, what should be the resulting difference of this input:

a,b,c,d,e,f

and

b,c,d,e,f,a

?