I wonder if the following case is possible in Unix using the diff function.

Let's say that I have one line

1234 1 2

and one file containing

1234 2 3

1234 represents the pid, 2 the disk usage and 3 the cpu usage for example. Is it possible, using the diff function, to 'merge' my line with the line in the file, and to apply a sum function on the fly to have the following final output? (pid being the merge key)

1234 3 5

(3 being the sum of 1 and 2, 5 being the sum of 2 and 3)


no, diff cannot do arithmetic calculations, use awk as following:

awk '{ grouping_disks[$1]+=$2; grouping_cpus[$1]+=$3 }
    for(indx in grouping_disks)
        print indx, grouping_disks[indx], grouping_cpus[indx]
}' file1 file2 fileN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.