counting non integer number in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting counting non integer number in awk
# 1  
Old 06-29-2009
counting non integer number in awk

Hi, I am having the following number in the file tmp
Code:
31013.004
20675.336
43318.190
30512.926
48992.559
277893.111
41831.330
8749.113
415980.576
28273.054

I want to add these numbers, I am using following script
Code:
awk 'END{print s}{s += $1}' tmp

its giving answer 947239 which is correct, but it is rounding off, and giving the number,

The exact answer is 947239.199, How can i get this answer by using awk script?

Please help me

Thanks,
Chaitanya

Last edited by Yogesh Sawant; 06-29-2009 at 05:43 AM.. Reason: added code tags
# 2  
Old 06-29-2009
# 3  
Old 06-29-2009
Or use printf
Code:
awk '
{s += $1 } 
END {
   printf "%.3f\n",s 
   }
' tmp

# 4  
Old 06-29-2009
Thank you very much

Chaitanya.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert string number to a integer

I have data as below "ROWS merge process complete. thousand rows changed" I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000. Any help or pointer. Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: dsravanam
6 Replies

2. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

3. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

4. Shell Programming and Scripting

awk counting number of occurences

Hi, I am trying to count the max number of occurences of field1 in my apache log example: 10.0.0.1 field2 field3 10.0.0.2 filed2 field3 10.0.0.1 field2 field3 10.0.0.1 field2 field3 awk result to print out only the most occurence of field1 and number of occurence and field1 is... (3 Replies)
Discussion started by: phamp008
3 Replies

5. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies

6. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 Replies

7. Shell Programming and Scripting

counting the number of lines - again

Hi all, I use bash shell and I have a problem with wc. I would like to determine the number of lines in a file so I do wc -l filename but I don't want to get the filename again I just would like to have the number of lines and use it in a variable. Can anybody help? Thank you, (7 Replies)
Discussion started by: f_o_555
7 Replies

8. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies

9. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 Replies

10. Linux

counting the number of lines

Hello, I have afile which begins with a few urls on multiple lines and then there is listing of some information on separate lines. The listing begins with the word Name on a given line followed by teh actual list. I want to count the number of lines in this file after the line having... (6 Replies)
Discussion started by: nayeemmz
6 Replies
Login or Register to Ask a Question