简体   繁体   中英

How to sum rows in a tsv file using awk?

My input:

Position   A   B   C   D   No
1   0   0   0   0   0
2   1   0   1   0   0
3   0   6   0   0   0
4   0   0   0   0   0
5   0   5   0   0   0

I have a TSV file, like the above, where I wish to sum the rows of numbers in the ABCD columns only, not the Position column. Desired output would have a TSV, two columns with Position and Sum in the first row,

Position   Sum
1    0
2    2
3    6
4    0
5    5

So far I have:

awk 'BEGIN{print"Position\tSum"}{if(NR==1)next; sum=$2+$3+$4+$5 printf"%d\t%d\n",$sum}' infile.tsv > outfile.tsv

Could you please try following, what you were trying to hard code field numbers which will not work in many cases so I am coming with a loop approach(where we are skipping first field and taking sum of all fields then).

awk 'FNR==1{print $1,"sum";next} {for(i=2;i<NF;i++){sum+=$i};print $1,sum;sum=""}' Input_file

Change awk to awk 'BEGIN{OFS="\\t"} rest part same of code in case you need output in TAB form.

You were very close, try this:

 awk 'BEGIN{print"Position\tSum"}{if(NR==1)next; sum=$2+$3+$4+$5; printf "%d\t%d\n",$1,sum; }' infile.tsv > outfile.tsv

But I say it's way cleaner with newlines and spaces:

awk '
BEGIN { 
    print"Position\tSum";
}
{  
    if (NR==1) {
        next; 
    }
    sum = $2 + $3 + $4 + $5 + $6; 
    printf "%d\t%d\n", $1, sum;
}'

极简脚本可以是

$ awk '{print $1 "\t" (NR==1?"Sum":$2+$3+$4+$5)}' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM