awk for total


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk for total
# 1  
Old 12-10-2012
awk for total

Have a input file like this .....

Code:
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 !
# 2  
Old 12-10-2012
Code:
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

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-10-2012
I guess you use the du command with -h option. Try -k

Code:
du -sk *

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 12-11-2012
Tested it successfully.
Code:
 
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,
This User Gave Thanks to Joseph_TKLee For This Post:
# 5  
Old 12-11-2012
... and with a brute force type casting:

Code:
awk '/M$/{tot+=$2/1024}/G$/{tot+=($2/1)}/T$/{tot+=$2*1024}END{printf "%.2f",tot}'ile

# 6  
Old 12-11-2012
I guess for completeness this should be expanded for B (bytes), K(Kilobytes) and P(Petabytes)..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk adjustment to print total

im trying to print all lines in the /var/log/syslog file that contain the pattern CRON. and after all the lines have been printed, i want a total of all the lines that contained "CRON" to be printed at the end. the below command is printing the correct lines, but it is giving me the sum of all... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Total Count using AWK

Hi Everybody, I have the following example file... 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1... (8 Replies)
Discussion started by: sraj142
8 Replies

3. Shell Programming and Scripting

Awk total and variance

File1 0358 Not Visible ***:* NA:NA RDF1+TDEV Grp'd (M) RW 102413 0359 Not Visible ***:* NA:NA RDF1+TDEV N/Grp'd (m) RW - 035A Not Visible ***:* NA:NA RDF1+TDEV N/Grp'd (m) RW - 035B Not Visible ***:* NA:NA ... (2 Replies)
Discussion started by: greycells
2 Replies

4. Shell Programming and Scripting

How to get total time using awk

Hi guys, I can't find a solution to sum the h:m:s: columns. 28/05/2010 03h 29min 34seg ADSL TELEMAR 28/05/2010 12h 19min 21seg ADSL TELEMAR 29/05/2010 04h 20min 02seg ADSL TELEMAR 29/05/2010 04h 31min 45seg ADSL TELEMAR 30/05/2010 06h 10min 43seg ADSL TELEMAR Thanks Use code... (8 Replies)
Discussion started by: ashimada
8 Replies

5. Shell Programming and Scripting

awk total and print if

hi all! I have a space delimited file... I would like to total column 3 on the condition that column 2 is less than 1030 to a variable in my script something like this: TOTAL="`awk '{$2 < 1030} {s+=$3}END{print s}' file`" Seem to be ignoring the {$2 < 1030}, I'm not sure of the... (3 Replies)
Discussion started by: lyoncc
3 Replies

6. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

7. Shell Programming and Scripting

Finding the total of a column using awk

Here is my file name countries USSR 8650 262 Asia Canada 3852 24 North America China 3692 866 Asia USA 3615 219 North America Brazil 3286 116 South America India 1269 637 Asia Argentina 1072 ... (8 Replies)
Discussion started by: ironhead3fan
8 Replies

8. Shell Programming and Scripting

awk script to calculate total

Hi First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears. I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines... (2 Replies)
Discussion started by: appsguy616
2 Replies

9. Shell Programming and Scripting

Awk help needed to calculate total

Hi all, I have a flat file like 10 steven 25 mike 47 Charles 127 Nancy 34 steven 23 mike 67 Charles 7761 Nancy 8 steven 54 mike 88 Charles 1267 Nancy I need to calculate the total of steven and all the members , for this I am using like grep "`sed -n 1p patterns.txt`"... (7 Replies)
Discussion started by: senthilkumar_ak
7 Replies
Login or Register to Ask a Question