Conversion if 1st column is match (awk '{s+=$1} END

Hi im trying to add numbers, got no problem with it im using

 awk '{s+=$1} END {print s, "MB"}',

but what if the numbers are like mention below. who will i add them

2000 KB
1 MB
Answer: 2001

Desired:
2000 KB
1 MB

Answer: 3000

Check $2 value and perform required arithmetic:

awk '{s+=($2=="MB"?$1*1024:$1)}END{print s " MB"}' file
1 Like

Well so far and your subject are clear on what you did: Considering only column 1...
So your "what if"... would be to look also at column 2, and depending what you find, add a conversion to the value in column 1...

1 Like

@VBE, yeah my bad,, was creating a script that will add my total diskpace.ttal memory,nuber of cpu, Flavor of OS, cpu model, server serial... this is my lack, the total disk space,

@yoda
thanks, big help for my basic script but will reduce a lot of time and effort

awk '                                                  
$2~/^[Kk]/ {s+=$1/1024}
$2~/^[Mm]/ {s+=$1}
$2~/^[Gg]/ {s+=$1*1024}
END {print s,"MB"}
' file