LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 05: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, 06:06 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 06: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, 06:34 AM   #4
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,669

Rep: Reputation: 498Reputation: 498Reputation: 498Reputation: 498Reputation: 498
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, 06:44 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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

You both are welcome!
 
  


Reply



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 12: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 04: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 09:51 AM
sum the numbers between and including a and b palpankaj Programming 3 02-18-2010 08:52 AM
Bash command to list installed libraries and version numbers newtovanilla Linux - Newbie 4 07-18-2008 04:49 PM

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

All times are GMT -5. The time now is 03:57 AM.

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
Open Source Consulting | Domain Registration