Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ksh convert MB/GB to number

2 views
Skip to first unread message

inetquestion

unread,
Oct 27, 2008, 2:04:09 PM10/27/08
to
On solaris 10 running the command: prctl -n project.max-shm-memory $$
| grep priv | awk '{print $2}'

gives a number such as 100Mb or 100Gb. I need to convert the answer
to a common format in my script in order to do calculations on it...
is there a clean way to do this besides using a bunch of if/then
statements to figure it out?

-Inet

Janis Papanagnou

unread,
Oct 27, 2008, 2:25:45 PM10/27/08
to

Not quite sure how you want to process the numbers, but these example
may inspire you...

$ x=100Mb ; echo ${x/Mb/000000} ; y=100Gb ; echo ${y/Gb/000000000}
100000000
100000000000

$ echo "100Mb 100Gb" | sed 's/Mb/000000/g;s/Gb/000000000/g'
100000000 100000000000

$ echo $'Hello 100Mb \n World 100Gb' |
awk '{gsub(/Mb/,"000000",$2);gsub(/Gb/,"000000000",$2);print $2}'
100000000
100000000000

The latter to resemble the usage of awk in your example above.

Mind that there are differing interpretation of what a 'Mega' or a
'Giga' or a 'kilo' actually is in the context of IT devices. So maybe
you prefer something like

$ echo $'Hello 100Mb \n World 100Gb' |
awk '{gsub(/Mb/,"*1024*1024",$2);
gsub(/Gb/,"*1024*1024*1024",$2);print $2}' | bc
104857600
107374182400

$ x=100Mb ; echo $(( ${x/Mb/*1024*1024} ))
104857600
$ y=100Gb ; echo $(( ${y/Gb/*1024*1024*1024} ))
107374182400


Janis

>
> -Inet

Glenn Jackman

unread,
Oct 27, 2008, 2:50:52 PM10/27/08
to
At 2008-10-27 02:25PM, "Janis Papanagnou" wrote:
[...]

> $ echo $'Hello 100Mb \n World 100Gb' |
> awk '{gsub(/Mb/,"000000",$2);gsub(/Gb/,"000000000",$2);print $2}'
> 100000000
> 100000000000
>
> The latter to resemble the usage of awk in your example above.
>
> Mind that there are differing interpretation of what a 'Mega' or a
> 'Giga' or a 'kilo' actually is in the context of IT devices. So maybe
> you prefer something like
>
> $ echo $'Hello 100Mb \n World 100Gb' |
> awk '{gsub(/Mb/,"*1024*1024",$2);
> gsub(/Gb/,"*1024*1024*1024",$2);print $2}' | bc
> 104857600
> 107374182400
>
> $ x=100Mb ; echo $(( ${x/Mb/*1024*1024} ))
> 104857600
> $ y=100Gb ; echo $(( ${y/Gb/*1024*1024*1024} ))
> 107374182400


See http://en.wikipedia.org/wiki/Binary_prefix for the gory details.

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

Stephane CHAZELAS

unread,
Oct 27, 2008, 2:51:40 PM10/27/08
to
2008-10-27, 11:04(-07), inetquestion:
[...]

prctl -n project.max-shm-memory "$$" |
perl -lape '
next unless /priv/;
$_ = $F[1];
s/(\d+)Gb/$1*2**30/ge;
s/(\d+)Mb/$1*2**20/ge'


--
Stéphane

Chris F.A. Johnson

unread,
Oct 27, 2008, 4:06:30 PM10/27/08
to

case $num in
*Kb) num=${num%Kb}000 ;;
*Mb) num=${num%Mb}000000 ;;
*Gb) num=${num%Gb}000000000 ;;
esac

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

inetquestion

unread,
Oct 27, 2008, 10:27:55 PM10/27/08
to
I need to convert a given value to MB, but the original can be in any
of the following formats: 6.89EB, 5.232GB, 234.2MB, 234B. Aside from
some ugly if blocks, is there an easy way to convert this to a
standard format/value?

-Inet

Message has been deleted

Michael Schindler

unread,
Oct 28, 2008, 9:19:54 AM10/28/08
to
inetquestion wrote:

prctl -n project.max-shm-memory $$ | \
nawk 'BEGIN {
b10arr["Kb"]=2^10;
b10arr["Mb"]=2^20;
b10arr["Gb"]=2^30;
b10arr["Tb"]=2^40;
b10arr["Pb"]=2^50;
}
/priv/ {
split($2, Z, /[a-zA-Z]+/);
split($2, B, /[0-9]+/);
print Z[1] * b10arr[B[2]];
} '

0 new messages