LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2012, 06:53 AM   #1
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Rep: Reputation: Disabled
Carry out an awk command to sum a list of numbers but include 0's


Hi, im trying to carry out an awk command to get the total sum of a file but need the 0's before the number to be included.

File (totals.txt)
0000000000000062.88
0000000000000110.14
0000000000000059.44
0000000000000051.09
0000000000000136.57
0000000000000203.28
0000000000000042.84
0000000000000729.02
0000000000000039.55
0000000000000030.95
0000000000000087.44
0000000000000028.98

the best i can do is the following:

awk '{ sum += $1 } END { print sum }' totals.txt

output:
1582.18

whereas i need it to come back as 0000000000001582.18

any ideas or help would be greatly appreciated,

Thanks
 
Old 12-05-2012, 07:06 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi and welcome to LinuxQuestions!

You can try printf and specify the desired format:
Code:
awk '{ sum += $1 } END{ printf "%019.2f\n", sum }' file
If you want to retrieve the format from the input, it's a little more tricky but doable.
 
2 members found this post helpful.
Old 12-05-2012, 07:24 AM   #3
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Colucix,

That is exactly what i needed!

Thanks a million
 
Old 12-05-2012, 07:34 AM   #4
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,665

Rep: Reputation: 487Reputation: 487Reputation: 487Reputation: 487Reputation: 487
Quote:
Originally Posted by colucix View Post
Hi and welcome to LinuxQuestions!

You can try printf and specify the desired format:
Code:
awk '{ sum += $1 } END{ printf "%019.2f\n", sum }' file
If you want to retrieve the format from the input, it's a little more tricky but doable.
Always comes with good solution. May you merit as much prosperity as you have done good to others in need.

Although I was not the one who solicited help, never the less I say, thank you.
 
Old 12-05-2012, 07:44 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Thank you, malekmustaq! Really appreciated.

You both are welcome!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to sum bytes, kilobytes, megabytes and gigabytes using awk command roopakl Linux - Newbie 8 11-30-2012 01:02 PM
[SOLVED] How-to include a variable's name in a regex in awk to spot command names in absolute paths Didier Spaier Programming 3 09-14-2012 05:05 AM
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 10:51 AM
sum the numbers between and including a and b palpankaj Programming 3 02-18-2010 09:52 AM
Bash command to list installed libraries and version numbers newtovanilla Linux - Newbie 4 07-18-2008 05:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration