AWK help to add up sequential values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK help to add up sequential values
# 1  
Old 08-20-2009
AWK help to add up sequential values

Hello All!
As a beginner user i want to add up sequential values in a text file and want to print total sum as output.The Text file will have values like the following:
class1{root}>less SUM.txt
1140.00 1155.00 1183.00 ... # it continues #

i tried to write a script with for-do looping and awk inside the loop but it didnt work (not a surprise to me Smilie ) And i dont know how to end up the looping as it should detect the last value in file (for ex: if there is 5 values it should stop when AWK detects that 6th value is zero). Could you help me please..


Code:
for (( i=1; i<=100; i++ ))   do  

cat SUM.txt  |  awk '{total += $i} END {printf "%15.2f\n",total}'

if  [ "$awk'{printf "%15.2f\n",total}" -eq  0 ]

then break  ## or end up the loop and print the result ##
fi
done


Last edited by EAGL€; 08-20-2009 at 02:30 PM..
# 2  
Old 08-20-2009
  1. To keep the forums high quality for all users, please take the time to format your posts correctly.
    1. Use Code Tags when you post any code or data samples so others can easily read your code.
      You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)
    2. Avoid adding color or different fonts and font size to your posts.
      Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
    3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

    Thank You.

    The UNIX and Linux Forums
    Reply With Quote

  2. Post sample data
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk use sequential line numbering in output

The awk below produces an output with the original header and only the matching lines (which is good), but the output where the original line numbering in the match found on is used. I can not figure out how to sequentially number the output instead of using the original. I did try to add... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to print missing and keep sequential ordering if not found

The below awk in bold will look for the ids in file1 in $2 of file2 and if they match print the line in file2. If an id is missing or not found in file2 (like BMPR2 in line4 of file1) I can not figure out how to add it to them to the lines in the output as missing in $3 following the same format.... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Add values of similar patterns with awk

so my output is this: session_closed=157 session_opened=151 session_closed=18 session_opened=17 there are two patterns here, but with different values. the two patterns are "session_opened" and "session_closed". i expect there will be many more other patterns. what i want to do is... (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

Add a number all the the values and log transform them using awk

Hi, is it possible to add a number to all the values in a file and log transform them using awk. thanx in advance input name c1 c2 c3 c4 r1 0 0.2 0.3 0.4 r2 0 0 0 1 r3 1 0 0 0 r4 0 0 1 ... (1 Reply)
Discussion started by: quincyjones
1 Replies

5. UNIX for Dummies Questions & Answers

[Solved] awk solution to add sequential numbers based on a word

Hi experts, I've been struggling to format a large genetic dataset. It's complicated to explain so I'll simply post example input/output $cat input.txt ID GENE pos start end blah1 coolgene 1 3 5 blah2 coolgene 1 4 6 blah3 coolgene 1 4 ... (4 Replies)
Discussion started by: torchij
4 Replies

6. Shell Programming and Scripting

Add markup tag and sequential number after specific line

Hello, This one has me a bit stumped. I have data the looks like, M END > <PREDICTION_ACCURACY> PROBABLE > <NO_OF_PARENTS> 3 > <CLOGP> -13.373 > <SMILES> OCC(O)C(OC1OC(CO)C(OC2OC(CO)C > <MIMW> 1006.322419888 (3 Replies)
Discussion started by: LMHmedchem
3 Replies

7. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

8. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

9. Programming

Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX

Writing a Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX I have over the years come across the same issue a couple of times, and it normally is that the read speed on SAN is absolutely atrocious when doing non-sequential I/O to the disks. Problem being of... (7 Replies)
Discussion started by: vrghost
7 Replies

10. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies
Login or Register to Ask a Question