3

I've the following problem: I would like to compare the content of 8 files contaning a list like this

Sample1.txt    Sample2.txt     Sample3.txt
apple          pineapple       apple
pineapple      apple           pineapple
bananas        bananas         bananas
orange         orange          mango
grape          nuts            nuts

using comm Sample1.txt Sample 2.txt I can have something like this

grape    nuts    apple
                 pineapple
                 bananas
                 orange

meaning that in the first column I have something related only to the first sample, the second column the things related only to the second sample and the third column the things in common.

I would like to do the same but with 8 files (sample). With diff it is not possible but at the end I would like to have

Sample1  Sample2   Sample3    ...Sample8     Things in common
grape    nuts      mango                     apple
                                             pineapple
                                             bananas

Is there a chance to do it with bash? Is there a command like diff that allow the searching for differences on more than two files?

Thank you to everybody...I know this is a challenging question

Fabio

fabioln79
  • 395
  • 2
  • 5
  • 16
  • you might be able to program that with comm and some bash scripting: compare file X to the concatenation of all other files, producing "only in file X", then concat all these "only in file X" files and compare that to the concatenation of all the files, producing the "common" part. It's probably not terribly efficient for large number of files though. – armel Apr 05 '13 at 06:41
  • How have you solved this? – Perlnika Nov 04 '13 at 03:12

1 Answers1

0

Here is my naive solution:

first=sample1.txt; for a in *.txt; do comm -12 $first $a >temp_$a; echo "comparing" $first " " $a "and writing to temp_$a"; first=temp_$a; cat temp_$a; done;
Perlnika
  • 4,796
  • 8
  • 36
  • 47