Meghan Rudd
Meghan Rudd

Reputation: 25

How to sum a selection of columns?

I'd like to sum multiple columns in a text file similar to this:

GeneA Sample  34  7  8   16
GeneA Sample  17  7  10  91
GeneA Sample  42  9  8   11

I'd like to generate the sum at the bottom of columns 3-5 so it will look like:

GeneA Sample  34   7   8   16
GeneA Sample  17   7  10   91
GeneA Sample  42   9   8   11
              93  23  26 

I can use this for a single column but don't know how to specify a range of columns:

awk -F'\t' '{sum+=$3} END {print sum}' input file> out

Upvotes: 0

Views: 60

Answers (4)

karakfa
karakfa

Reputation: 67507

$ awk -v OFS='\t' '{s3+=$3; s4+=$4; s5+=$5; $1=$1} 1; 
              END  {print "","",s3,s4,s5}' file

GeneA   Sample  34      7       8       16
GeneA   Sample  17      7       10      91
GeneA   Sample  42      9       8       11
                93      23      26

Upvotes: 1

flu
flu

Reputation: 556

Try this. Note that NF just means number of fields. And AWK indexing starts with 1. So the example here has a range of 3 to the last col.

awk '{ for(i=3;i<=NF;i++) sum[i] += $i } END { for(i=3;i<=NF;i++) printf( "%d ", sum[i] ); print "" }' input_file

If you want fewer columns, say 3 and 4, then I'd suggest:

awk '{ for(i=3;i<=4 && i<=NF;i++) sum[i] += $i } END { for(i=3;i<=4 && i<=NF;i++) printf( "%d ", sum[i] ); print "" }' input_file

Upvotes: 0

James Brown
James Brown

Reputation: 37404

In awk:

$ awk '
{
    for(i=3;i<=NF;i++)                       # loop wanted fields
        s[i]+=$i }                           # sum to hash, index on field #
END { 
    for(i=3;i<=NF;i++)                       # same old loop
        printf "%s%s",s[i],(i==NF?ORS:OFS) } # output
' file
93 23 26 118

Currently the for loop goes thru every numeric field. Change the parameters if needed.

Upvotes: 1

Andriy Berestovskyy
Andriy Berestovskyy

Reputation: 8544

The easiest way is just repeat summing for each column, i.

awk -F '\t' '{
    s3 += $3
    s4 += $4
    s5 += $5
}
END {
    print s3, s4, s5
}' input_file > out

Upvotes: 1

Related Questions