Sum of Columns

HI Guys,

I gave Input file F.Txt

ID	H1	H2	H3	H4	H5
A	5	6	7	8	9
B	4	65	4	4	7
C	4	4	4	4	4
D	4	4	4	4	4

Output :-

ID	H1	H2	H3	H4	H5
Total	17	79	19	20	24

Sum of Each Columns

This isn't much different from lots of other requests that have been posted here...

awk '
BEGIN {	OFS = "\t"
}
FNR == 1 {
	print
	nf = NF
	next
}
{	for(i = 2; i <= nf; i++)
		sum += $i
}
END {	printf("Total")
	for(i = 2; i <= nf; i++)
		printf("%s%d", OFS, sum)
	print ""
}' F.txt

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

awk 'END{$1="Total";for(i=1;i++<NF;){$i=sum};print}{for(i=1;i++<NF;i){sum+=$i}}NR==1' OFS="\t" F.txt

@danmero: for(i=1;i++<NF;) or for(i=1;i++<NF;i) does not fly for BSD awk:

awk: illegal statement
 input record number 1, file infile
 source line number 1

Also: if (one or more of) the header fields start with a number, then the output will be wrong

--
While we are surfing:

awk 'END{$0=t; $1="Total"; print} FNR>1{for(i in T) $i+=T; split(t=$0, T)}FNR==1' OFS='\t' file

Hi Scrutinizer, you are right about array traversal.
------
Regarding the loop , works on my BSD

# uname -a
FreeBSD sn.route 8.3-RELEASE-p16 FreeBSD 8.3-RELEASE-p16 #0: Mon Aug 25 08:25:41 EDT 2014
# echo | awk 'END{for(;i++<5;)print i}' 
1
2
3
4
5
# echo | awk 'END{for(;++i<=5;)print i}'
1
2
3
4
5

.... I should work on my scripting portability, thanks for pointing out :cool:

Hi Danmero, good to see you back here :slight_smile: ..
I looked into it and the culprit is the variable i in the third part, perhaps because it is not an operation? So

$ awk 'BEGIN{for(;i++<5;)print i}' 
1
2
3
4
5

works, but

$ awk 'BEGIN{for(;i++<5;i)print i}' 
1
awk: illegal statement
 source line number 1

Does not..

Whereas

$ awk 'BEGIN{for(;i++<5;i=i)print i}' 
1
2
3
4
5

Does...

Good to see you, I have to come back to refresh my memory from time to time :smiley:

i is not an action.

i=i, this will assign the value of i variable to i variable, useless acction.

I know, but that is what you used in post #4

Yes, it was just to illustrate..

Ops, my error on on post 4 , however originally run on Linux and no errors , later on BSD yield error

# uname -a
Linux ela.2.4.b 2.6.18-348.1.1.el5 #1 SMP Tue Jan 22 16:24:03 EST 2013 i686 i686 i386 GNU/Linux
# awk --version | head -2
GNU Awk 3.1.5
Copyright (C) 1989, 1991-2005 Free Software Foundation.
# cat file
ID      H1      H2      H3      H4      H5
A       5       6       7       8       9
B       4       65      4       4       7
C       4       4       4       4       4
D       4       4       4       4       4
# # awk 'END{$1="Total";for(i=1;i++<NF;){$i=sum};print}{for(i=1;i++<NF;i){sum+=$i}}NR==1' OFS="\t" file
ID      H1      H2      H3      H4      H5
Total   17      79      19      20      24

# uname -a
FreeBSD sn.route 8.3-RELEASE-p16 FreeBSD 8.3-RELEASE-p16 #0: Mon Aug 25 08:25:41 EDT 2014 
# awk --version
awk version 20110810 (FreeBSD)
# cat file
ID      H1      H2      H3      H4      H5
A       5       6       7       8       9
B       4       65      4       4       7
C       4       4       4       4       4
D       4       4       4       4       4
# awk 'END{$1="Total";for(i=1;i++<NF;){$i=sum};print}{for(i=1;i++<NF;i){sum+=$i}}NR==1' OFS="\t" file
awk: illegal statement
 input record number 1, file file
 source line number 1