-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?

4
  • What did you do to solve your problem?
    – gxx
    Jan 21, 2016 at 9:07
  • I want to use shell or perl to solve it
    – jython.li
    Jan 21, 2016 at 9:11
  • Alright...and what do you did so far?
    – gxx
    Jan 21, 2016 at 9:11
  • 1
    I want to use sed "s/reg/str/g",but sed not support arithmetic in 'str'
    – jython.li
    Jan 21, 2016 at 9:19

1 Answer 1

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
1
  • That's what I want, thks!
    – jython.li
    Jan 22, 2016 at 7:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .