Shell script to sum every 3 values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to sum every 3 values
# 1  
Old 02-23-2012
Shell script to sum every 3 values

I am looking for an easy way to calculate the sum of three values (*-12, *-01, *-02) which are contained after a comma (,). I have found an awk command that will sum every 3rd value, but I am not interested in the values before the comma (,).

Code:
 awk '{s+=$1}NR%3==0{print s;t+=s;s=0}'

I am only interested in summing every 3rd value after the comma (,). If it helps, this 3rd value will contain a -02 in the line.

Example:
1944-12,5.6
1945-01,9.8
1945-02,6.7
1945-03,9.3
1945-04,5.9
1945-05,0.7
1945-06,0.0
1945-07,0.0
1945-08,0.0
1945-09,0.0
1945-10,0.2
1945-11,10.5
1945-12,22.3
1946-01,35.4
1946-02,13.4

Any help is appreciated.
# 2  
Old 02-23-2012
So what is the expected output for that sample data?
# 3  
Old 02-23-2012
From my understanding of your question, is this what you're looking for?
Code:
awk -F, '/....-(12|01|02)/{s+=$2}END{print s}' inputfile

# 4  
Old 02-23-2012
Code:
 TOTAL=0;for i in $(grep -- '-02,' tmp.dat | cut -d\, -f2);do TOTAL=$( echo $TOTAL + $i | bc -l  );done; echo $TOTAL

Assumes that the -02 value is present (not the case in your sample data, or you have a different meaning of every 3rd value Smilie )
# 5  
Old 02-23-2012
Ideally, the output would be in the following format -

1945-02,22.1
1946-02,77.1
# 6  
Old 02-23-2012
Try:
Code:
awk -F"[-,]" '$2==12{a[$1+1]+=$3}$2<3{a[$1]+=$3}END{for (i in a) print i"-02,"a[i]}' file

# 7  
Old 02-23-2012
Bartus

That is calculating the correct values, but it is out order. There are 'M' in some places where the values are, so that could be mucking with the script.

2005-02,2.9
2006-02,2.9
2007-02,5.1
2008-02,2.7
2009-02,1.4
1960-02,13.1
1948-02,7
1961-02,17.3
1949-02,16.8
1962-02,10.1
1963-02,3.3
1964-02,3.1
1965-02,2.9
1966-02,8.1
1967-02,0.7
1980-02,1.8
1968-02,9.3
1981-02,0
1969-02,2.6
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need Optimization shell/awk script to aggreagte (sum) for all the columns of Huge data file

Optimization shell/awk script to aggregate (sum) for all the columns of Huge data file File delimiter "|" Need to have Sum of all columns, with column number : aggregation (summation) for each column File not having the header Like below - Column 1 "Total Column 2 : "Total ... ...... (2 Replies)
Discussion started by: kartikirans
2 Replies

2. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

3. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

4. Shell Programming and Scripting

Shell script to sum up the space allocated to filesystems

Hi , I Would like to know the space allocated by adding up all the allocated space to group of filesystems .. example , df -h|grep /db | awk '{ print $4 }' ---> giving me all the used space on the filesystem but need to know the total used space by adding up all the values (3 Replies)
Discussion started by: nsankineni
3 Replies

5. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

6. Shell Programming and Scripting

Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through... Script that i am using is below : #!/bin/bash sum=0 for i in $@ do sum=$sum+$1 echo $sum shift done I am executing the script as... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

7. Shell Programming and Scripting

Sum up values of columns in 4 files using shell script

I am new to shell script.I have records like below in 4 different files which have about 10000 records each, all records unique and sorted based on column 2. 1 2 3 4 5 6 --------------------------- SR|1010478|000044590|1|0|0| SR|1014759|000105790|1|0|0| SR|1016609|000108901|1|0|0|... (2 Replies)
Discussion started by: reach.sree@gmai
2 Replies

8. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

9. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

10. UNIX for Dummies Questions & Answers

Shell script to calc sum of bytes used by files

I'm looking to create a Korn Shell script that, if given a directory as an arg, will calc bytes used by all files in the given directory and display that info. If no command line arg is given the program is to calc and display the bytes used by all the files in the pwd. Example output: ... (3 Replies)
Discussion started by: kecannon
3 Replies
Login or Register to Ask a Question