This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Awk Help

Dear All,

I am new to awk programming, so I was trying to add the contents of the table. I have a table like this

e_gw1.100.18.1  244

e_gw1.100.2.1   12

e_gw1.100.2.1   433

e_gw1.100.22.1  15

e_gw1.100.24.1  151

e_gw1.100.24.1  47

e_gw1.100.33.1  326

And I want something like this:

e_gw1.100.18.1  244

e_gw1.100.2.1   445

e_gw1.100.24.1 198

e_gw1.100.33.1  326

I am trying to make this table unique by adding all its associated values. Thanks in advance.

CS

awk

Off topic. Difficult to understand what the OP wants.

not exactly bioinformatics related, but:

awk '{ a[$1]=a[$1]+$2 } END{ for(k in a){ print k, a[k]} }'

Sorry , but this is something I was doing to get the count of reads to features.

Thanks for your help

2 answers

awk '{data[$1] += $2} END{for (key in data) {print key, data[key]}}' foo.txt
e_gw1.100.22.1 15
e_gw1.100.33.1 326
e_gw1.100.24.1 198
e_gw1.100.18.1 244
e_gw1.100.2.1 445

Bedtools has a groupby function for this as well (docs: http://bedtools.readthedocs.org/en/latest/content/tools/groupby.html) :

bedtools groupby -g 1 -c 2 -o sum -i foo.txt
e_gw1.100.18.1    244
e_gw1.100.2.1    445
e_gw1.100.22.1    15
e_gw1.100.24.1    198
e_gw1.100.33.1    326

thanks very much

You should ask these kind of questions on a specific programming forum, like Stack Overflow.

Log in to answer this question.