I have a file file1 with the amount of times a user shows up in the files, something like this:4 userC2 userA1 userBand I have another file file2 with users and other info like:userC, degree2userA, degree1userB, degree2and I want an output where it shows the amount of times user shows up, for each degree:5 degree22 degree1
Summing by common strings in different files
shell script;text processing;awk;numeric data
Pure awk:$ awk -F'[, ]' 'NR==FNR{n[$2]=$1;next}{m[$3]+=n[$1]}    END{for(i in m){print i   m[i]}}' \    file1 file2degree1 2degree2 5Or you can put it into a script like this:#!/usr/bin/awk -f BEGIN {    FS=[, ]}{    if (NR == FNR) {        n[$2] = $1;        next;    } else {        m[$3] += n[$1];    }}END {    for (i in m) {        print i   m[i];    }}First set field separator to both comma and space (that is the BEGIN block or the -F command line option.Then, when parsing the first file (the FNR == NR idiom) put number of connections for a user into array indexed by user name. When parsing the following file(s), add the number of connections for each user into the array indexed by user group.Finally (the END block) scan the whole array and print the key, value pairs.