Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have (multiple) files like below list. Few of them has both the values in TB and couple of files in GB. Now if you clearly see the file output there are two rows with Total capacity and Capacity used. I can do this with awk to add up both the rows separately but I see few are in GB. Would it be possible to write script which will check if values are in GB or TB, if it's in GB then convert to TB (1024).

[root@localhost maneesh]# cat server1.txt
Total capacity                   2.0 TB
Capacity used                    735.23 GB
[root@localhost maneesh]#

Basically, at the end I want all my values to be presented in TB.

Any help on this would be appreciated.

share|improve this question
    
Whats have you been able to do so far? Where do you need help? –  AbhishekGirish Nov 30 '12 at 5:45
    
I have awk script which can add up both the values for all the servers. If I use that, I would be getting wrong numbers, as I have few servers where capacity used rows are in GB –  maneeshshetty Nov 30 '12 at 5:48
    
There is one simple solution that i can think of - I'll add it as an answer if it works for you - cat server*.txt |grep -i GB will list those values which have GB - you can probably redirect output to another file. Similarly for those in TB. You could then add both the totals, after you have converted. –  AbhishekGirish Nov 30 '12 at 6:06
1  
Since you have awk set-up, why can't you do an if statement in awk to check the $3? –  nerdwaller Nov 30 '12 at 6:20
1  
@maneeshshetty: which software created server1.txt? –  akira Nov 30 '12 at 6:30

1 Answer 1

up vote 3 down vote accepted

You could check if $4 is GB and divide $3 by 1024 in case it is. If infile contains:

Total capacity                   2.0 TB
Capacity used                    735.23 GB

You can do something like this (parse.awk):

/Total capacity/ { if($4 == "GB") $3 /= 1024; tot  += $3 }
/Capacity used/  { if($4 == "GB") $3 /= 1024; used += $3 }

END { print "Total: " tot " TB, Used: " used " TB" }'

And if you run it like this:

awk -f parse.awk infile infile

You get:

Total: 4 TB, Used: 1.436 TB
share|improve this answer
    
@Thor- Thanks for the awk code. It worked for me. I only had to remove ' which was showing me as invalid character. I have put now as END { print "Total: " tot " TB, Used: " used " TB" } –  maneeshshetty Dec 1 '12 at 9:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.