How to find out the maximum cumulative value?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to find out the maximum cumulative value?
# 1  
Old 06-29-2016
How to find out the maximum cumulative value?

I have a file has thousands of rows and each row has a number and the number can be positive or negative. I want to do the cumulative sum from the first row. How can I find out the maximum cumulative value when I do the sum work row by row. Here is the example:
Code:
4
-3
2
-3
-1
1

In this case, the maximum cumulative value is 3 which is from adding the first 3 rows numbers (4, -3 and 2)
Thank you very much
# 2  
Old 06-29-2016
Is this a homework assignment? Homework and coursework questions can only be posted in the homework and coursework forum with a completely filled out questionnaire from the homework rules.

If this is homework, please refile in the proper forum with a completely filled out template. If it is not homework, please explain what the purpose is for this problem.

And why do you think the maximum cumulative value is 3 when the accumulated value after reading the first line is 4?

What code have you tried to solve this problem?

What shell and operating system are you using?
# 3  
Old 06-29-2016
This is not homework or assignment. I am doing some accounting work (many transaction) to report the highest balance on the banking account and the bank statement didn't provide the cumulative balance number to me, so I have to figure out a way to find it.
The reason I didn't count the first row because the adding work doesn't start. I don't do code work so I posted a help on this forum probably I posted the wrong place. Sorry.



Quote:
Originally Posted by Don Cragun
Is this a homework assignment? Homework and coursework questions can only be posted in the homework and coursework forum with a completely filled out questionnaire from the homework rules.

If this is homework, please refile in the proper forum with a completely filled out template. If it is not homework, please explain what the purpose is for this problem.

And why do you think the maximum cumulative value is 3 when the accumulated value after reading the first line is 4?

What code have you tried to solve this problem?

What shell and operating system are you using?
# 4  
Old 06-29-2016
Would this do?
Code:
awk '{print $1, SUM+=$1}' file3
4 4
-3 1
2 3
-3 0
-1 -1
1 0

# 5  
Old 06-29-2016
If you need the cumulative maximum along with the line number where it occurs:

Code:
#! /bin/ksh

typeset -i iCurrLine=0
typeset -i iMaxLine=0
typeset -i iMaxSum=0
typeset -i iVal=0
typeset -i iSum=0

while read iVal ; do
     (( iCurrLine += 1 ))
     (( iSum += iVal ))
     if [ $iSum -gt $iMax ] ; then
          iMaxLine=$iCurrLine
          iMaxSum=iSum
     fi
done < /path/to/input

print - "Max Value: $iMaxSum \t occurred in line: $iMaxLine"

exit 0

Should work equally in bash.

I hope this helps.

bakunin
# 6  
Old 06-29-2016
I'm afraid I misread your request. How about
Code:
awk '{print $1, SUM+=$1, MAX=MAX<SUM?SUM:MAX}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

Find minimum and maximum values based on column with associative array

Hello, I need to find out the minimum and maximum values based on specific column, and then print out the entire row with the max value. Infile.txt: scf6 290173 290416 . + X_047241 T_00113118-1 scf6 290491 290957 . + X_047241 T_00113118-2 scf6 290898 290957 . + X_047241 T_00113119-3 scf6... (2 Replies)
Discussion started by: yifangt
2 Replies

3. Shell Programming and Scripting

Find the maximum value and take value from the neigbouring cells

Let say I have this table A B 0.30 C D 0.60 E F 0.80 G H 0.11 I J 0.10 K L 0.23 M N 0.50 O P 0.01 I need to find the maximum value in each row and output the highest value plus the information two columns back. For example: the output should look like this E F 0.80 M N 0.50 Thanks in... (2 Replies)
Discussion started by: seiksoon
2 Replies

4. UNIX for Dummies Questions & Answers

Using awk to find and use the maximum value in column of data

Dear Unix Gurus, I have a text file with multiple columns, for example, see sample.txt below 0 1 301 1 4 250 2 6 140 3 2 610 7 1 180I want to find the maximum in, say, column 3, normalise all the values to this maximum value (to 4 decimal places) and spit everything into a new... (2 Replies)
Discussion started by: tintin72
2 Replies

5. Shell Programming and Scripting

Find the maximum of a value

0.01 0.6 0.39 0.4 0.3 0.3 1 0 0 0 0 1 I would like to print 2 if the maximum of a row is the first column I would like to print 1 if the maximum of a row is the second colum print 0 if it is the third. so in this case, 0.6 is the max of the first row so i would want to print 1... (5 Replies)
Discussion started by: johnkim0806
5 Replies

6. Homework & Coursework Questions

Find the Maximum value and average of a column

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: I am trying to complete a script which will allow me to find: a) reads a value from the keyboard. (ask the... (4 Replies)
Discussion started by: dstewie
4 Replies

7. UNIX for Dummies Questions & Answers

Please help me to find out maximum value of a field based on grouping of other fields.

Please help me to find out maximum value of a field based on grouping of other fields, as we do in SQL. Like in SQL if we are having below records : Client_Name Associate_Name Date1 Value C1111 A1111 2012-01-17 10 C1111 A1111 ... (1 Reply)
Discussion started by: KamalKumarKalra
1 Replies

8. Shell Programming and Scripting

TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!! My Requirement is............. i have a input file: 501,501.chan 502,502.anand 503,503.biji 504,504.raja 505,505.chan 506,506.anand 507,507.chan and my o/p should be chan->3 i.e. the word which occurs maximum number of times in a file should be... (5 Replies)
Discussion started by: aajan
5 Replies

9. UNIX for Dummies Questions & Answers

Find out the maximum growing file in a mount

I need to find the file that is growing in the mount. Say yesterday the utilised space was 95% but today that is 96%. How do i find the file that is growing in size. Have checked the same with du/df options but was not able to find much. Please suggest the best possible option. (3 Replies)
Discussion started by: raman1605
3 Replies

10. UNIX for Dummies Questions & Answers

How to find the maximum # of PIDs

Is there a command in HP Unix which can be used inside a K shell to find out the maximum number of processes (PIDs) a pc can generate? Any help will be greatly appreciated. Steve (8 Replies)
Discussion started by: stevefox
8 Replies
Login or Register to Ask a Question