LinuxQuestions.org
Help answer threads with 0 replies.
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
 
Thread Tools
Old 11-07-2007, 09:28 PM   #1
BrianK
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?
BrianK is offline     Reply With Quote
Old 11-07-2007, 10:48 PM   #2
angrybanana
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:
Code:
445.76 Kb

Last edited by angrybanana; 11-07-2007 at 11:12 PM.. Reason: changed to printf for output (for rounding)
angrybanana is offline     Reply With Quote
Old 11-08-2007, 05:28 AM   #3
bigearsbilly
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
bigearsbilly is offline     Reply With Quote
Old 11-08-2007, 01:29 PM   #4
BrianK
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 View Post
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:
Code:
445.76 Kb
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..
BrianK is offline     Reply With Quote
Old 11-08-2007, 02:22 PM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147
Thanked: 2
Quote:
Originally Posted by BrianK View Post
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.
angrybanana is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Static awk or gawk binary rbautch Linux - Software 4 06-12-2007 10:26 PM
LXer: Get started coding with GAWK and AWK LXer Syndicated Linux News 0 09-26-2006 03:54 PM
Webalizer - howto convert the trafic into human readable? sys7em Linux - Server 3 09-25-2006 05:48 AM
Deleting a line with gawk/awk caps_phisto Linux - General 4 11-06-2004 03:31 PM
awk strange behaviour in tcsh flyingalex Linux - Software 5 10-17-2003 11:27 AM


All times are GMT -5. The time now is 06:53 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration