Have a input file like this .....
attachments 100G
shared 1T
archive 300M
documents 300G
remotedocs 150M
I need the total in GB's ... where as M=MB,G=GB,T=TB
Basically need awk to calculate the total based on end suffix
Thanks !
Yoda
2
awk ' BEGIN { total_gb=0; } {
if ($2 ~ /M/) { gsub("M","",$2); total_gb+=($2/1024); }
if ($2 ~ /G/) { gsub("G","",$2); total_gb+=$2; }
if ($2 ~ /T/) { gsub("T","",$2); total_gb+=($2*1024); }
} END { printf "%0.2fG\n", total_gb;
} ' infile
1 Like
rdcwayx
3
I guess you use the du command with -h option. Try -k
du -sk *
1 Like
Tested it successfully.
cat input.txt | awk '{ if ( $2 ~ /T$/ ) Total+=substr($2,1,length($2)-1)*1000000; else if ( $2 ~/G$/ ) Total+=substr($2,1,length($2)-1)*1000; else Total+=substr($2,1,length($2)-1) } END { print Total "MB"}'
Cheers,
1 Like
ripat
5
... and with a brute force type casting:
awk '/M$/{tot+=$2/1024}/G$/{tot+=($2/1)}/T$/{tot+=$2*1024}END{printf "%.2f",tot}'ile
I guess for completeness this should be expanded for B (bytes), K(Kilobytes) and P(Petabytes)..
1 Like