Q:

script to convert GB to TB

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.

A:

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

