I have a tab-separated file1 like this (subset shown, real matrix 60x60000):
rowname header1 header2 header3 header4 header5 header6 header7 header8
rowname1 1 1 10 2 3 1 10 2
rowname2 0 7 200 3 37 1 2 1
And another file2 like this:
header1,header2
header3
header4,header5
header6,header8
header7
I would like to sum columns specified in each line of file2:
rowname header1 header3 header4 header6 header7
rowname1 2 10 5 3 10
rowname2 7 200 40 2 2
So column1+column2, column3 as it is, column4+column5, column6+column8, column7 as it is...
Some columns must be summed, others not, and not always the columns to be summed are consecutive.
If the columns are summed, the header of the first column needs to remain in the output file.
I am wondering if you have a solution with awk. So far I just know how to store header entries:
awk '
NR==1 {
for (i=1; i<=NF; i++) {
f[$i] = i
}
header
followed by an increasing number or should we take the header names from the second file?key1
,key2
, etc. have to do with it?