-1

As follows, some text

16G    16G    1.9G    40G
4G     4G     952M    60G
16G    16G    1.6G    40G
5G     780M   5G      80G

I want to change all the unit from 'M' to 'G', like this

16G    16G    1.9G    40G
4G     4G     0.92G   60G
16G    16G    1.6G    40G
5G     0.76G  5G      80G

I can use python to do it, but I don't know how to achieve it with shell? Can awk,sed,perl... do it?

jython.li
  • 84
  • 2
  • 10

1 Answers1

0
root@lab7-dl380-11:~/> cat test
16G 754G 457M
346M 77G 654M

root@lab7-dl380-11:~/>  awk '{for(i=1;i<=NF;i++)if($i ~ /M/)printf $i/1024 "G\t"; else printf $i "\t"; print ""}' test
16G     754G    0.446289G
0.337891G       77G     0.638672G

Also you can round to hundredth for smarter output

root@lab7-dl380-11:~/>  awk '{for(i=1;i<=NF;i++)if($i ~ /M/)printf int($i/1024*100)/100 "G\t"; else printf $i "\t"; print ""}' test
16G     754G    0.44G
0.33G   77G     0.63G
user1700494
  • 1,642
  • 2
  • 11
  • 20