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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
11-07-2007, 09:28 PM
#1
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian et al
Posts: 1,284
Thanked: 7
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)?
[
Log in to
get rid of this advertisement]
in a script, I run through a file system finding certain files and accumulating their size.
At the end of the script, it generates a report, but the accumulated sizes are HUGE.
I'm woefully ignorant when it comes to [g]awk. Is there a simple way to convert the large number into something more human redable?
11-07-2007, 10:48 PM
#2
Member
Registered: Oct 2003
Distribution: Archlinux
Posts: 147
Thanked: 2
example code:
Code:
awk 'BEGIN{sum=456456;
hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb";
for (x=1024**3; x>=1024; x/=1024){
if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break }
}}'
output:
Last edited by angrybanana; 11-07-2007 at 11:12 PM ..
Reason: changed to printf for output (for rounding)
11-08-2007, 05:28 AM
#3
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,663
Thanked: 14
Code:
du -sh *
94M HOME.tgz
32K noughts.zip
146M solaris_home_20070814.tgz
linux only
11-08-2007, 01:29 PM
#4
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian et al
Posts: 1,284
Thanked: 7
Original Poster
Quote:
Originally Posted by
angrybanana
example code:
Code:
awk 'BEGIN{sum=456456;
hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb";
for (x=1024**3; x>=1024; x/=1024){
if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break }
}}'
output:
Factastic, thanks!
I had to remove the "BEGIN" in order to be able to access data passed in through $1, otherise, this works great.
Thanks again.
Last edited by BrianK; 11-08-2007 at 01:31 PM ..
11-08-2007, 02:22 PM
#5
Member
Registered: Oct 2003
Distribution: Archlinux
Posts: 147
Thanked: 2
Quote:
Originally Posted by
BrianK
Factastic, thanks!
I had to remove the "BEGIN" in order to be able to access data passed in through $1, otherise, this works great.
Thanks again.
Oh yea, used "BEGIN" for demonstration sake (no input). You'll probably use it in "END". Also, I forgot to add in 'bytes', but that should be trivial.
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 06:53 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News
LQ Podcast
LQ Radio