Sum of Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of Columns
# 1  
Old 02-26-2016
Sum of Columns

HI Guys,

I gave Input file F.Txt

Code:
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 :-


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

Sum of Each Columns
# 2  
Old 02-26-2016
This isn't much different from lots of other requests that have been posted here...
Code:
awk '
BEGIN {	OFS = "\t"
}
FNR == 1 {
	print
	nf = NF
	next
}
{	for(i = 2; i <= nf; i++)
		sum[i] += $i
}
END {	printf("Total")
	for(i = 2; i <= nf; i++)
		printf("%s%d", OFS, sum[i])
	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.
# 3  
Old 02-27-2016
Code:
awk 'END{$1="Total";for(i=1;i++<NF;){$i=sum[i]};print}{for(i=1;i++<NF;i){sum[i]+=$i}}NR==1' OFS="\t" F.txt

# 4  
Old 02-27-2016
@danmero: for(i=1;i++<NF;) or for(i=1;i++<NF;i) does not fly for BSD awk:
Code:
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:
Code:
awk 'END{$0=t; $1="Total"; print} FNR>1{for(i in T) $i+=T[i]; split(t=$0, T)}FNR==1' OFS='\t' file

# 5  
Old 02-28-2016
Hi Scrutinizer, you are right about array traversal.
------
Regarding the loop , works on my BSD
Code:
# 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 Smilie

Last edited by danmero; 02-28-2016 at 09:30 AM..
# 6  
Old 02-28-2016
Quote:
Originally Posted by danmero
Hi Scrutinizer, you are right about array traversal.
------
Regarding the loop , works on my BSD
Code:
# 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 Smilie
Hi Danmero, good to see you back here Smilie ..
I looked into it and the culprit is the variable i in the third part, perhaps because it is not an operation? So
Code:
$ awk 'BEGIN{for(;i++<5;)print i}' 
1
2
3
4
5

works, but

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

Does not..

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

Does...

Last edited by Scrutinizer; 02-28-2016 at 10:34 AM..
# 7  
Old 02-28-2016
Quote:
Originally Posted by Scrutinizer
Hi Danmero, good to see you back here Smilie ..
Good to see you, I have to come back to refresh my memory from time to time Smilie

Quote:
I looked into it and the culprit is the variable i in the third part, perhaps because it is not an operation? So
Code:
$ awk 'BEGIN{for(;i++<5;i)print i}' 
1
awk: illegal statement
 source line number 1

Does not..
i is not an action.
Quote:
Code:
$ awk 'BEGIN{for(;i++<5;i=i)print i}'

Does...
i=i, this will assign the value of i variable to i variable, useless acction.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Group by columns and add sum in new columns

Dear Experts, I have input file which is comma separated, has 4 columns like below, BRAND,COUNTRY,MODEL,COUNT NIKE,USA,DUMMY,5 NIKE,USA,ORIGINAL,10 PUMA,FRANCE,DUMMY,20 PUMA,FRANCE,ORIGINAL,15 ADIDAS,ITALY,DUMMY,50 ADIDAS,ITALY,ORIGINAL,50 SPIKE,CHINA,DUMMY,1O And expected output add... (2 Replies)
Discussion started by: ricky1991
2 Replies

2. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

3. Shell Programming and Scripting

Sum of all columns

Hi Friends, I have a file with fields separated with comma. How to print sum of each field of the file? Eg: input file 1,3,6,7 2,1,2,1 0,1,1,0 I want to sum each field separately. Output file 3,5,9,8 Thanks, Suresh (2 Replies)
Discussion started by: suresh3566
2 Replies

4. Shell Programming and Scripting

Get the SUM of TWO columns SEPARATELY by doing GROUP BY on other columns

My File looks like: "|" -> Field separator A|B|C|100|1000 D|E|F|1|2 G|H|I|0|7 D|E|F|1|2 A|B|C|10|10000 G|H|I|0|7 A|B|C|1|100 D|E|F|1|2 I need to do a SUM on Col. 5 and Col.6 by grouping on Col 1,2 & 3 My expected output is: A|B|C|111|11100 (2 Replies)
Discussion started by: machomaddy
2 Replies

5. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

6. Shell Programming and Scripting

Sum columns

Hi All, I'm new to this forum. So please be patience with me! :) I have a file that looks like this (all rows have the same number of columns): 19 20 30 15 17 38 51 60 74 85 96 07 .... 10 20 44 59 39 88 13 77 30 10 11 12 .... . . . I want to sum the value of first field to all the... (2 Replies)
Discussion started by: Aderson Nascime
2 Replies

7. UNIX for Dummies Questions & Answers

Sum of a columns in vi/vim

Hi, i want to find out the sum of a column in vi/vim not by using any other editor or filter like sed/awk. Is there any function available to do the sum of a column. let cat>number.txt one 1 two 2 five 5 nine 9 here i want the sum of second field is 17, is there any function to... (3 Replies)
Discussion started by: Manabhanjan
3 Replies

8. Shell Programming and Scripting

Sum of three columns - in 4N columns file

Hi All, happy new year. I have a file with 4xN columns like 0.0000e+00 0.0000e+00 7.199E+07 7.123E+07 6.976E+07 6.482E+07 5.256E+07 2.523E+07 0.0000e+00 0.0000e+00 8.641E+07 8.550E+07 8.373E+07 7.780E+07 6.309E+07 3.028E+07... (8 Replies)
Discussion started by: f_o_555
8 Replies

9. Shell Programming and Scripting

sum of three columns

Hi All, I have like this M17XX-050-01 0100000000 QQSSS 0.0000e+00 1.712E+06 1.255E+07 0.0000e+00 0.0000e+00 1.722E+06 1.263E+07 0.0000e+00 ... 0.0000e+00 1.204E+06 8.829E+06 0.0000e+00 M17XX-050-01 0100000000 WWSSS 0.0000e+00 7.564E+03 1.165E+01 0.0000e+00... (6 Replies)
Discussion started by: f_o_555
6 Replies

10. Shell Programming and Scripting

awk sum columns

can anyone help me how do i add the colums using awk seperated by character @. for eg i have 3@4 2@9 5@1 the result should be 10 14 i tried using { sum+= $1 } END { print sum } but it just gives the result 10. can anyone help me with this one thank you and best regards (7 Replies)
Discussion started by: phone_book
7 Replies
Login or Register to Ask a Question