Visit Our UNIX and Linux User Community


Sum of all lines in file without roundup with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of all lines in file without roundup with awk
# 1  
Old 08-12-2009
Sum of all lines in file without roundup with awk

Hi,

I have a file and I want to sum all the numbers in it.

Example of the file:

Code:
0.6714359
-3842.59553830551

I used your forum (https://www.unix.com/shell-programmin...ines-file.html) and found a script, what worked for me:

Code:
awk '{a+=$0}END{print a}' output.txt > sum.txt

But the script rounds up my sum and the sum.txt file is like that:

Code:
-3841.92

I would like the sum.txt file to be that way:

Code:
-3841.92410240551

How to sum up the lines in the file without round-up?

Thank you in advance,

Mario
# 2  
Old 08-12-2009
Use printf instead of print:

Code:
printf("%f\n",a)

Regards
# 3  
Old 08-12-2009
Quote:
Originally Posted by Franklin52
Code:
printf("%f\n",a)

For me it only solved half of the problem, because now the output file is like this:

Code:
-3841.924102

Better then before Smilie .
# 4  
Old 08-12-2009
You can change the default of 6 decimals to 11 like this:

Code:
printf("%f.11\n",a)

# 5  
Old 08-12-2009
Or you could use Perl:

Code:
$
$ cat f2
0.6714359
-3842.59553830551
$
$ awk '{a+=$0}END{print a}' f2
-3841.92
$
$ perl -lne '{chomp;$x+=$_}END{print $x}' f2
-3841.92410240551
$
$

tyler_durden
# 6  
Old 08-12-2009
Thank you both, I will try them as soon as I get to work Smilie .

---------- Post updated at 01:01 PM ---------- Previous update was at 12:49 PM ----------

Quote:
Originally Posted by Franklin52
You can change the default of 6 decimals to 11 like this:

Code:
printf("%f.11\n",a)

The ".11" addition only adds it in the end of the number. Like this:

Code:
-3841.924102.11

I'll try the pearl advice aswell.

---------- Post updated at 01:43 PM ---------- Previous update was at 01:01 PM ----------

Quote:
Originally Posted by durden_tyler
Code:
$
$ cat f2
0.6714359
-3842.59553830551
$
$ awk '{a+=$0}END{print a}' f2
-3841.92
$
$ perl -lne '{chomp;$x+=$_}END{print $x}' f2
-3841.92410240551
$
$

If I use the script:

Code:
perl -lne '{chomp;$x+=$_}END{print $x}' f2

alone, then it works, but if I add it to the other row's then it never finishes. My complete sh script is:

Code:
grep 'zero point VIBRATIONAL energy' */* > nr001.txt
grep 'total energy' > nr002.txt
awk 'NR==FNR {print $8; next} {print $6}' nr001.txt nr002.txt > nr003.txt
perl -lne '{chomp;$x+=$_}END{print $x}' nr003.txt > nr004.txt

# 7  
Old 08-12-2009
Quote:
Originally Posted by mario8eren
The ".11" addition only adds it in the end of the number. Like this:

Code:
-3841.924102.11

Sorry a typo, should be:

Code:
printf("%.11f\n",a)


Previous Thread | Next Thread
Test Your Knowledge in Computers #302
Difficulty: Easy
The GNU website is the official repository for Slackware.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sum value of a column by range defined in another file awk?

I have two files, file1.table is the count table, and the other is the range condition file2.range. file1.table chr start end count N1 0 48 1 N1 48 181 2 N1 181 193 0 N1 193 326 2 N1 326 457 0 N1 457 471 1 N1 471 590 2 N1 590 604 1 N1 604 752 1 N1 752 875 1 file2.range... (12 Replies)
Discussion started by: yifangt
12 Replies

2. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Sum duplicate values in text file through awk between dates

I need to sum values in text file in case duplicate row are present with same name and different value below is example of data in file i have and format i need. Data in text file 20170308 PM,U,2 PM,U,113 PM,I,123 DA,U,135 DA,I,113 DA,I,1 20170309 PM,U,2 PM,U,1 PM,I,123 PM,I,1... (3 Replies)
Discussion started by: Adfire
3 Replies

4. Shell Programming and Scripting

awk file sum results

I ran this script: #!/bin/bash ./disk-usage-JM.pl > jm_out InFile="jm_out" OutFile="jm_temp" awk '{if (!match(" \t",substr($0,1,1))) {server=$0; next} if (substr($0,1,1)==" ") {workspace=$0; next} print server,workspace,$0}' \ $InFile >$OutFile rm jm_out against this... (2 Replies)
Discussion started by: master-of-puppe
2 Replies

5. UNIX for Dummies Questions & Answers

awk to sum column field from duplicate row/lines

Hello, I am new to Linux environment , I working on Linux script which should send auto email based on the specific condition from log file. Below is the sample log file Name m/c usage abc xxx 10 abc xxx 20 abc xxx 5 xyz ... (6 Replies)
Discussion started by: asjaiswal
6 Replies

6. Shell Programming and Scripting

Sum of 286th column using awk in a file

Hi, I am using the following code to find the sum of the values of column 286 in a file. It will have the Decimal values with the scale of 2. Delimiter is '|^' cut -d'|^' -f286 filename|cut -c3-| awk '{ x += $1 } END { printf("%.2f\n", x) }' There are around 50k records in this file... (2 Replies)
Discussion started by: Jram
2 Replies

7. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum

Hi friends, This is sed & awk type question. It is slightly different from my previous question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers (but no more than 10 numbers in series) whenever i find it and produce an output file with the... (4 Replies)
Discussion started by: kaaliakahn
4 Replies

8. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

9. Shell Programming and Scripting

Sum value from selected lines script (awk,perl)

Hello. I face this (2 side) problem. Some lines with this structure. ........... 12345678 4 12345989 13 12346356 205 12346644 74 12346819 22 ......... The first field (timestamp) is growing (or at least equal). 1)Sum the second fields if the first_field/500 are... (8 Replies)
Discussion started by: paolfili
8 Replies

10. Shell Programming and Scripting

how to get the sum of all the lines in the file

Hi I have the following file, how I will calculate the sum of all the entries in the file. > cat abc 2 3 4 now the sum should be 2+3+4 = 9 (4 Replies)
Discussion started by: sdosanjh
4 Replies

Featured Tech Videos