Creating a file from a list and several different files

Asked

Viewed 135 times

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 on the list. These are the type 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 name of the list in the file, print column 2, if not find print 0..

Can anyone help?

1 answer

0

Suppose 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. Possibly ...| 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 .......
  • Thanks Joao, it worked perfectly for 2 files, but I have 600... I’m trying to edit here, but help is always welcome!

  • @Silvia, And you want to write 600 different columns?

  • Yes, this is my sample size (hence the despair... rs)

  • But I already got a solution in the forum in English... I will post the link. Thank you very much!

  • http://stackoverflow.com/questions/43853049/combining-several-text-files-in-a-comparative-way/43855774#43855774

  • Did you ever see the hiccup I proposed yesterday?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.