Adding previous results


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding previous results
# 1  
Old 04-18-2012
Adding previous results

Hi,

I am trying to use awk to do the following:

For a column:

Code:
5
3
4
2
7

I want to start with the first entry then add the second to the first, third to second ..etc:

Code:
5
8
12
14
21

Any thought of how that can be done in awk?

Thanks!
# 2  
Old 04-18-2012
assuming your column is 1 use following

Code:
awk '{x+=$1} END {print x}' infile

This User Gave Thanks to 47shailesh For This Post:
# 3  
Old 04-18-2012
This will give me the sum of all. But what I want is the sum of each entry + Previous to form a column like the one obove. Is that doable?
# 4  
Old 04-18-2012
47shailesh's script can easily be enhanced to achieve that:
Code:
awk '{x+=$1;print x} END {print x}' infile

This User Gave Thanks to jlliagre For This Post:
# 5  
Old 04-18-2012
Thanks that worked Smilie
I have one last question about this, if I want to apply the method to a set o columns (about 125!) do I have to do it for one by one or there is a way to do them all at once.

Thank you!
# 6  
Old 04-19-2012
Code:
awk '{for(i=1;i<=NF; i++)
          {
             mx=(i>mx)? i : mx; x[i]+=$i
             printf("%d" ", x[i])
          } 
          printf("\n")
        }  
           END { for(i=1;i<=mx; i++) 
                    { 
                        printf("%d" ", x[i])  
                    }
                    printf("\n")
                  }' infile

if your numbers are floating point (not integers) use "%f" instead of "%d"
in the printf() functions.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies

2. UNIX for Beginners Questions & Answers

Adding results up

Hi There Just created a .sql script and executes fine, it comes back with two lines of results, I was wondering is there a way of adding up the two results to get a round number. I tired wc -l but that didn't work. Many Thanks for your help psql -t -f... (3 Replies)
Discussion started by: simpsa27
3 Replies

3. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

4. Shell Programming and Scripting

Date - incorrect results for previous date

Hello: I am bit puzzled with what I could be doing wrong and any help is appreciated. I have a date in YYYMMDD format and I need to find the previous date. Based on the input on this forum, I have come up with the following. It seems to work for all except the following. Here I am passing date... (3 Replies)
Discussion started by: wincrazy
3 Replies

5. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

6. Shell Programming and Scripting

Adding Previous Month To Filename

Dear experts, I'm using solaris 5.10 and bash. I want to zip file "Amount.txt" to "Amount.zip" and rename it to "Amount_<prev_month>_<this year>.zip". For example, file for this month should be renamed to "Amount_06_2012.zip", for next month it should be "Amount_07_2012.zip". I have no problem... (8 Replies)
Discussion started by: kris.adrianto
8 Replies

7. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

8. Shell Programming and Scripting

Adding grep'd results in a variable

Here is one I am baffled with; I have not used unix for a while and now that I am back it has been fun remembering and I have enjoyed it, for the most past. this is in ksh. I need to search in a file for the line with X1 and cut columns 20-25, put them into a variable, added them (dollar... (3 Replies)
Discussion started by: CougarMutt
3 Replies

9. Shell Programming and Scripting

Adding results of a find to an array

I'm trying to add the paths of all the xml files in certain directories to an array. I want to use the array later in my code. Anyway, for some reason this isn't working. Any help would be appreciated. Path_Counter=0 for result in "find * -name '*.xml'"; do XmlPath="$result" echo... (2 Replies)
Discussion started by: Fly_Moe
2 Replies

10. Shell Programming and Scripting

doing a for loop adding up the results

Hi there If I run a 'swap -l' on my solaris box, i get swapfile dev swaplo blocks free /dev/dsk/c1t0d0s1 54,65 8 67119560 65655144 /dev/dsk/c1t0d0s2 54,65 8 33119522 32655122 I wanted to run a for loop adding up the totals of each column 4 , excluding the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question