gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)?
ProgrammingThis 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.
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.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
This is great !!!
I do have one question
My source is a find and the total size of all found files is greater than 1 Gb. This is ofcourse no problem for this code.. But here is the thing. When the total size is above 1Mb the result is given in Mb's as wel as Kb's . Even when the total size is above 1Gb the results are in Gb's Mb's and Kb's
Any idea how to filter this so that it will only display the correct one?
Could you post an example of what you actually tried to do? The above awk code shows 1 format, be it Gb, Mb or Kb. It does not show Mb and Kb or Gb, Mb and Kb.
One more thing that could be of interest: You posted this from a machine running OS-X. Assuming that the code you tried is also run on that machine: What flavour/version of awk are you using? (awk --version should show both).
I do need some help.. I am quite a noob on Bash and awk
Code:
find ./ -name "*.jpg" -exec ls -l {} \; | awk '{for (I=9;I<=NF;I++){ printf "%s",$I }print " :",$5}{ x++ } { size=size+$5 } END { print "\n" x " Files Found""\n\n" } END { hum[1024**3]="Gb"; hum[1024**2]="Mb"; hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024) { if (size>=x) print "\n Total Size= " size/x,hum[x]; } }'
This is the script I am working on.. So I put the code for size in Human readable form at the end (with some minor changes). This works but it gives the 3 outputs (Gb, Mb, Kb) When I add the 'break' at the end (see original code) there is no output of the size.
So I do need some help with this. Maybe you can push me in the right direction..
2nd question: I use print is stead of printf cause I want to print some normal text with the output. I can't seem to fix that with printf..
MAny thanks
Edit: it seems it has something to do with total size.. In my test dir I have only 2 .jpg's with a total size of 228850 bytes. When I run the script (with the ';break' at the end, there is no result. When I creat a fake .jpg with a size of 1Gb the output works good. Also when I make a fake file of 25Mb no output is displayed.
Edit2: ok, I am a bit further.. It has something to do with using print in stead of printf..... when I use printf the output works great.. But How can i put some additional text in the output? So it displays Total Size = ......
Because the output of ls -l is not always the same when it comes to the date field, I changed it to ls -l --time-style=long-iso, which does give you a consistent output and a fixed starting field (8).
Whilst I like druuna's solution I have been reading a little about the vagaries of ls and why it should not be used, part of which is pointed out by druuna.
So I would submit this slight variation on the same theme:
Code:
#!/bin/bash
find . -name "*.awk" -exec stat {} \; |
awk '/Size:/{sum+=$2}
END{split("B KB MB GB TB PB",type);
for(i=5;y < 1;i--)
y = sum / (2**(10*i));
print "Total amount of files : "(NR/7)" - total size : "y type[i+2]
}'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.