LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-22-2013, 03:39 AM   #1
Rava
LQ Newbie
 
Registered: May 2010
Posts: 10

Rep: Reputation: 0
Question awk: Convert large number to human readable (Mb,Gb, TB, PB) / round up/down results


I read all posts of this thread http://www.linuxquestions.org/questi...gb-etc-597921/
Quote:
Originally Posted by grail View Post
Code:
#!/usr/bin/awk -f

BEGIN{	x = 456456

        split("B KB MB GB TB PB",type)

	for(i=5;y < 1;i--)
	    y = x / (2**(10*i))

	print y type[i+2]
}
Now, I wanted the above as script, and it was wikipedia that gave me the info. xD
Code:
#!/usr/bin/awk -f

BEGIN{	x = ARGV[1]

        split("B KB MB GB TB PB",type)

	for(i=5;y < 1;i--)
	    y = x / (2**(10*i))

	print y " " type[i+2]
}
(I also added a whitespace)

My example number is "3203368960", and my script gave me
Code:
2.98337 GB
Now... all that is still needed is a way to have the script round up/round down the calculation and cut all behind the dot values but the first 2, in my example that would be:
Code:
2.98 GB
If the result was
Code:
2.98533 GB
instead, the rounding should have been
Code:
2.99 GB
of course. Any ideas how this can be done with awk?

Last edited by Rava; 02-23-2013 at 04:38 AM.
 
Old 02-22-2013, 04:12 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 749
Blog Entries: 2

Rep: Reputation: 194Reputation: 194
I think you want the format options of printf(). http://linux.die.net/man/3/printf

printf("%.2f %s\n", number, unit);
 
1 members found this post helpful.
Old 02-22-2013, 04:14 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Try changing this line print y " " type[i+2] to this:
Code:
 printf("%.2f %s\n",y,type[i+2])
Also have a look at these 2:
- 5.5 Using printf Statements for Fancier Printing
- 13.2.3 Rounding Numbers

EDIT: Beaten to it
 
1 members found this post helpful.
Old 02-23-2013, 04:37 AM   #4
Rava
LQ Newbie
 
Registered: May 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks a lot to you! *hands over virtual TUX plushies*

Just for the record and for the ones lurking here / ones that find the thread via forum search or internet search, this is the finale version of the script:

Code:
#!/usr/bin/awk -f

BEGIN{	x = ARGV[1]

        split("B KB MB GB TB PB",type)

	for(i=5;y < 1;i--)
	    y = x / (2**(10*i))

	printf("%.2f %s\n",y,type[i+2])
}
 
  


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
How to convert a .bz2 file to a human-readable form in Windows 7 Professional Deedee393 Linux - Newbie 6 05-11-2012 07:57 AM
[SOLVED] Convert field from epoch to human readable sebelk Programming 6 01-08-2012 02:13 AM
Awk to convert bytes to human number? Xeratul Linux - General 7 10-22-2011 07:10 AM
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)? BrianK Programming 23 10-30-2010 04:20 AM
Webalizer - howto convert the trafic into human readable? sys7em Linux - Server 3 09-25-2006 04:48 AM


All times are GMT -5. The time now is 04:01 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration