Creating a file from a list and several different files

2

I have a list of names and several different files that may or may not contain all the names in the list and a value assigned to the names. I need to compare the values of all these each file to the names in the list. These are the kind of txt files I have

List

Alligator mississippiensis
Anas platyrhynchos
Anolis carolinensis
Chrysemys picta
Columba livia
Gallus gallus

File1

Alligator mississippiensis 2546
Anas platyrhynchos 32
Columba livia 21571
Gallus gallus 226

File2

Anas platyrhynchos 2
Anolis carolinensis 3255
Chrysemys picta 225
Columba livia 2215
Gallus gallus 22548

And this is the output format I need, where the first line would be the name of the files, indicating where the values came from ...

List File1, File2
Alligator mississippiensis 2546,0
Anas platyrhynchos 32,2
Anolis carolinensis 0,3255
Chrysemys picta 0,225
Columba livia 21571, 2215
Gallus gallus 226, 22548

I thought of something like grep and finding the list name in the file, print column 2, if it does not find print 0 ..

Can anyone help?

    
asked by anonymous 05.05.2017 / 22:45

1 answer

0

Assuming you have a recent awk (GNU-awk example of linux systems)

awk '    { a[$1 " " $2][FILENAME]=$3}
     END { printf("0: Animais %s, %s\n", ARGV[2], ARGV[3]);
           for (x in a ){
              printf("%s %d,%d\n", x, a[x][ARGV[2]], a[x][ARGV[3]])}
         }' lista f1 f2

This solution can change the order of the animals. Eventually ...| sort

EDIT : If we have a variable number of files, we can for example:

awk '    { a[$1 " " $2][FILENAME]=$3}
     END { printf("0: Animais"); 
           for(i=2; i< ARGC; i++) printf(",%s ",ARGV[i]); 
           printf("\n");
           for (x in a ){
              printf("%s", x); 
              for(i=2; i< ARGC; i++) printf(",%d",a[x][ARGV[i]]);
              printf("\n");}
         }' lista f1 f2 f3 f4 f5 .......
    
07.05.2017 / 22:42