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 :slight_smile: ) 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..

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
  1. To keep the forums high quality for all users, please take the time to format your posts correctly.
    [list=i]
  2. 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 and by hand.)
  3. 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.
  4. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
    [/list]
    Thank You.

    The UNIX and Linux Forums
    Reply With Quote

  5. Post sample data