Script to find out the sum of output on the screen

Hi,
I am new to scripting.I am using redhat linux 6.I am just finding a way to summing up the output displayed as below.It will help me a lot since we used to get this kind of output every now and then.

I am pasting here the output .What is required is this o/p needs to be summed up and final value should be displayed.

for example:

# lsvdisk | grep -vi cap | awk '{print $8}'
1.57TB
1.57TB
2.00TB
70.00GB
90.00GB
70.00GB
70.00GB

output should be displayed like:few of them are TB and few of them are in GB.
might be a seperate output for TB and GB is also fine.
300 GB and 5 TB .
Here i have pasted a sample.But in real time i used to get around 50 to 60 lines of output.
Appreciate your help on this.I am just finding a way from my end too.Advance thanks for your replies

Try something like:

awk '{t+=$8/($8~/GB/?1024:1)} END{print t, "TB"}'

It could also be further combined with the grep statement (and further refined if there are also MB or EB, for example)

1 Like
lsvdisk | awk '
        !/[Cc][Aa][Pp]/ {
                match ( $8, /[A-Z]*$/, A )
                R[A[0]] += $8
        }
        END {
                for ( k in R )
                        print R[k], k
        }
'
1 Like

Thanks alot.It's awesome.It is working as expected.
Once again very very thanks