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.
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).
Hope this helps.
Hi,
Thanks for the output.
I have mixed it with my current script and now it works just fine. Many thanks.
the date format did not work on my Apple. I will look into that.
So maybe I should start a new thread.. but since it is still converting to human readable I figured it is still on topic
So the first part is fixed. output works just fine ..
Next I want to see which users holds how many files (of let's say .jpg) and display the total size of those files
The script so far :
Code:
#!/usr/bin/env bash
#
find ./ -iname "*.jpg" -exec ls -l {} \; | awk 'BEGIN {
# Initialize all Arrays
size = "0";
u_count[""]=0;
all_count[""]=0;
}
{
# Assign field names
sizes=$5
user=$3
# Count of number of files
u_count[user]++;
all_count["* *"]++;
# Count disc space used
u_size[user]+=sizes;
all_size["* *"]+=sizes;
}
{
for (I=9 ; I<=NF ; I++) { printf "%s",$I } print " :",$5}{ x++ } { size=size+$5 }
END { print "\n" x " Files Found""\n" }
END { hum[1024**4]="Tb"; hum[1024**3]="Gb"; hum[1024**2]="Mb"; hum[1024]="Kb";
for (x=1024**4; x>=1024; x/=1024) { if (size>=x) { { printf "Total Size = ",NR }
printf "%.2f %s\n\n",size/x,hum[x];break }
}
# Output
for (i in u_count) {
if (i != "") {
print u_size[i], u_count[i], i;
}
}
for (i in all_count) {
if (i != "") {
print all_size[i], all_count[i], i;
}
}
} '
Ok ok, There are some double outputs.. (that will be fixed later, it is just for references)
The part where I need some help is in the table output. The total size is now displayed in bytes.. And I want that output the same as the total size which is stated above.. (in Gb/Mb/Kb....)
So in words.. I want the 'u_size[i]' & 'all_size[i]' to be printed in Human readable.
Well the first comment would be that either you use druuna's extended options of the ls command or ditch the ls -l altogether as it is inferior
and very prone to error. (check here)
Quote:
So in words.. I want the 'u_size[i]' & 'all_size[i]' to be printed in Human readable.
So what is the issue? Awk has the ability to create functions so just create one that does your calculation and the print the output.
If your not sure how to create a function then look here.
Well the first comment would be that either you use druuna's extended options of the ls command or ditch the ls -l altogether as it is inferior
and very prone to error. (check here)
So what is the issue? Awk has the ability to create functions so just create one that does your calculation and the print the output.
If your not sure how to create a function then look here.
hmm strange, it works now... although I have to fix some tabs..
First of All sorry for my english, I have a question, I'm editing a cgi script that counts data downloaded by users and shows speeds a chived by them, but the data is in bytes for download data/upload data and speeds, I tried serval things to print them as kB or mB but simply outputs are braking after my mods, what schould I point to :
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.