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
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
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
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
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
-Inet
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]];
} '