Only -k4 and with -r the header would be the footer.
Try to add | "sort -k4" to the every print/printf statements in awk except those printing the header/footer.
Ok: this is tricky..
If I put the sort command else where I receive an error :
awk: syntax error at source line 41
context is
} >>> | <<< sort -k4
awk: illegal statement at source line 42
My guess is I have to seperate my header / footer from the data.
(will have to figure that out :P)
This is the complete array output (FYI) :
{
for (I=9 ; I<=NF ; I++) { x++ } { size=size+$5 }
}
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
{ FS = ":";
format = "%11s %6s %-16s\n";
prinft "\n"
printf ( format, "Size","Count","Who" ) }
for (i in u_count) {
if (i != "") {
{ hum[1024**4]="Tb"; hum[1024**3]="Gb"; hum[1024**2]="Mb"; hum[1024]="Kb";
for (x=1024**4; x>=1024; x/=1024) { if (u_size>=x) {
usersize = sprintf ( "%.2f %s", u_size/x,hum[x] )
printf ( format,usersize, u_count, i);break } } } | sort -k4
}
}
for (i in all_count) {
if (i != "") {
{ hum[1024**4]="Tb"; hum[1024**3]="Gb"; hum[1024**2]="Mb"; hum[1024]="Kb";
for (x=1024**4; x>=1024; x/=1024) { if (all_size>=x) {
allsize = sprintf ( "%.2f %s", all_size/x,hum[x] )
printf ( format,allsize, all_count, "Total");break } } }
}
}
}'
If it's already a large stand-alone script invoked with a '#!/usr/bin/awk -f' I'd personally add a sort function, or try gawk's if you're OK with being dependent on it.
Edit: I guess it will print to screen seeing yazu's example. I was just reading the forums on lunch and didn't have a terminal to play with. :\
#!/usr/bin/env bash
#
#
# Bash Script written by R. Blaas
#
# This script will find files of a specific type and displays the full path and size
# Also a total of found files and size is displayed per Department and an overal total found files and size
#
# if nothing is passed to the script, show usage and exit
[[ -n "$1" ]] || { echo �Usage: usage.sh [Variable]�; exit 0 ; }
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Variables
POSTMASTER="My email"
DATE=`date +"%d%m%Y"`
DATIME=`date +"%Y%m%d%H%M"`
DAGNAAM=`date +"%A"`
# Set current directory to variable $CURRENT
CURRENT=/opt/local/COMPANY/usage
# Set the log directory
DIR_LOG=$CURRENT"/log"
# Catch search variable
SEARCH=`echo $1 | sed 's/*//' | sed 's/.//'`
# Check if directory exists
if test ! -d "$DIR_LOG"
then
mkdir "$DIR_LOG"
fi
# Create temporary file for log messages
POSTMLOG=$DIR_LOG"/"$SEARCH"-usage.log"
# BEGIN
logger "Script start (usage.sh)"
echo `date` start of script usage.sh > $POSTMLOG
echo "" >> $POSTMLOG
echo Search variable = $SEARCH >> $POSTMLOG
echo "" >> $POSTMLOG
# Search Command where $1 is the type to find. Search will start at current location.
find . -iname $1 -exec ls -l {} \; | awk 'BEGIN {
# Initialize all Arrays
size = "0";
u_count[""]=0;
all_count[""]=0;
}
{
# Assign field names
sizes=$5
split($9,a,"/")
dept=a[2]
# Count of number of files
u_count[dept]++;
all_count["* *"]++;
# Count disc space used
u_size[dept]+=sizes;
all_size["* *"]+=sizes;
}
{
for (I=9 ; I<=NF ; I++) { x++ } { size=size+$5 }
}
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=9 ; I<=NF ; I++) { x++ } { size=size+$5 }
}
END { { FS = ":";
format = "%11s %6s %-16s\n";
prinft "\n"
printf ( format, "Size","Count","Who" ) }
for (i in u_count) {
if (i != "") {
{ hum[1024**4]="Tb"; hum[1024**3]="Gb"; hum[1024**2]="Mb"; hum[1024]="Kb";
for (x=1024**4; x>=1024; x/=1024) { if (u_size>=x) {
usersize = sprintf ( "%.2f %s", u_size/x,hum[x] )
printf ( format,usersize, u_count, i);break } } }
}
}
}
{
for (I=9 ; I<=NF ; I++) { x++ } { size=size+$5 }
}
END { for (i in all_count) {
if (i != "") {
{ hum[1024**4]="Tb"; hum[1024**3]="Gb"; hum[1024**2]="Mb"; hum[1024]="Kb";
for (x=1024**4; x>=1024; x/=1024) { if (all_size>=x) {
allsize = sprintf ( "%.2f %s", all_size/x,hum[x] )
printf ( format,allsize, all_count, "Total");break } } }
}
}
}' >> $POSTMLOG
echo "" >> $POSTMLOG
echo `date` end of search >> $POSTMLOG
echo `date` send report to POSTMASTER >> $POSTMLOG
# Mail report to POSTMASTER
mail -s " usage.sh REPORT `date`" $POSTMASTER < $POSTMLOG
if [ "$?" == "0" ]; then
echo Mail sent successful! >> $POSTMLOG
else echo Mail sent unsuccesful! >> $POSTMLOG
fi
echo "" >> $POSTMLOG
echo `date` end of script usage.sh >> $POSTMLOG
echo "" >> $POSTMLOG
# Preserving logfile
mv "$POSTMLOG" "$DIR_LOG/$DATIME-$SEARCH-usage.log"
logger "Script end (usage.sh)"
Hmm the sort function still doesn't work how I like to see it.. But maybe it is just impossible in the current code.. Will work on it. I think I have enough info.
A different question:
On the split command, it seems that spaces are not accounted for.
For some of the fields seperated by the / the outcome should be something like: "my department". But instead I get only "my". I have done some search on this and in all occasions I see that spaces are allowed and stored/printed.. So what am I doing wrong here ?