gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)?
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? |
example code:
Code:
awk 'BEGIN{sum=456456; Code:
445.76 Kb |
Code:
du -sh * |
Quote:
I had to remove the "BEGIN" in order to be able to access data passed in through $1, otherise, this works great. Thanks again. |
Quote:
|
Quote:
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? |
ok no one with any suggestion(s)
|
Hi,
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). Hope this helps. |
Quote:
Hmm Turns out I must have an error in my format.. When I use a big number in your test script it works just fine.. So the script is good :) Nevermind me asking :) |
Glad to read you got it sorted out :)
|
Code:
#!/usr/bin/awk -f |
Quote:
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]; } }' 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 = ...... |
Hi,
Using grail's code posted in post #11: Code:
#!/bin/bash Hope this helps. |
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 |
I think you can easily get the file size with this command:
Code:
find -type f -name '*.jpg' -printf '%s\n' |
All times are GMT -5. The time now is 05:34 AM. |