ksh convert MB/GB to number - Unix
This is a discussion on ksh convert MB/GB to number - Unix ; 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
...
-
ksh convert MB/GB to number
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
-
Re: ksh convert MB/GB to number
inetquestion writes:
> 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?
I'd use strtod. This will give you double and a pointer to first
character that is not part of the number. I think this is safe even
though EB looks like the start of an exponent (3.5E-2). Follow up
with a switch based on the following character to determine the
multiplier:
double to_mb(const char *s)
{
char *ep;
double v = strtod(s, &ep);
if (ep > s) {
switch (tolower((unsigned char)*ep)) {
case 'b':
v *= 1e-6;
break;
case 'k':
v *= 1e-3;
break;
case 'g':
v *= 1e3;
break;
case 't':
v *= 1e6;
break;
case 'e':
v *= 1e9;
break;
}
}
return v;
}
I can image you want some error-checking in there but there is not
point in my guessing what or how you want to handle it.
--
Ben.
-
Re: ksh convert MB/GB to number
Ben Bacarisse writes:
>inetquestion writes:
>
>> 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?
>
>I'd use strtod. This will give you double and a pointer to first
>character that is not part of the number. I think this is safe even
>though EB looks like the start of an exponent (3.5E-2). Follow up
>with a switch based on the following character to determine the
>multiplier:
>
>double to_mb(const char *s)
>{
> char *ep;
> double v = strtod(s, &ep);
> if (ep > s) {
> switch (tolower((unsigned char)*ep)) {
> case 'b':
> v *= 1e-6;
> break;
> case 'k':
> v *= 1e-3;
> break;
> case 'g':
> v *= 1e3;
> break;
> case 't':
> v *= 1e6;
> break;
> case 'e':
> v *= 1e9;
> break;
> }
> }
> return v;
>}
>
>I can image you want some error-checking in there but there is not
>point in my guessing what or how you want to handle it.
>
This will execute in the korn shell? (see subject line).
scott
-
Re: ksh convert MB/GB to number
scott@slp53.sl.home (Scott Lurndal) writes:
> Ben Bacarisse writes:
>>inetquestion writes:
>>
>>> 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?
>>
>>I'd use strtod. This will give you double and a pointer to first
>>character that is not part of the number. I think this is safe even
>>though EB looks like the start of an exponent (3.5E-2). Follow up
>>with a switch based on the following character to determine the
>>multiplier:
>>
>>double to_mb(const char *s)
>>{
>> char *ep;
>> double v = strtod(s, &ep);
>> if (ep > s) {
>> switch (tolower((unsigned char)*ep)) {
>> case 'b':
>> v *= 1e-6;
>> break;
>> case 'k':
>> v *= 1e-3;
>> break;
>> case 'g':
>> v *= 1e3;
>> break;
>> case 't':
>> v *= 1e6;
>> break;
>> case 'e':
>> v *= 1e9;
>> break;
>> }
>> }
>> return v;
>>}
>>
>>I can image you want some error-checking in there but there is not
>>point in my guessing what or how you want to handle it.
>>
>
> This will execute in the korn shell? (see subject line).
No, obviously it's C.
This is why it's a bad idea to put important information only in the
subject line. It's easy to overlook it when concentrating on the body
of the message to write a reply. Now Ben has wasted a bunch of time
writing a function that doesn't help you, and you didn't get your
question answered. Always make the message body self-contained, and the
subject a summary of it.
With regard to your question, I'm not aware of any slick way to do it.
I think you just have to do it by hand, as you suspected.
-
Re: ksh convert MB/GB to number
Nate Eldredge writes:
> scott@slp53.sl.home (Scott Lurndal) writes:
>
>> Ben Bacarisse writes:
>>>inetquestion writes:
>>>
>>>> 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?
>>>
>>>I'd use strtod. This will give you double and a pointer to first
>>>character that is not part of the number. I think this is safe even
>>>though EB looks like the start of an exponent (3.5E-2). Follow up
>>>with a switch based on the following character to determine the
>>>multiplier:
>>>
>>>double to_mb(const char *s)
>>>{
>>> char *ep;
>>> double v = strtod(s, &ep);
>>> if (ep > s) {
>>> switch (tolower((unsigned char)*ep)) {
>>> case 'b':
>>> v *= 1e-6;
>>> break;
>>> case 'k':
>>> v *= 1e-3;
>>> break;
>>> case 'g':
>>> v *= 1e3;
>>> break;
>>> case 't':
>>> v *= 1e6;
>>> break;
>>> case 'e':
>>> v *= 1e9;
>>> break;
>>> }
>>> }
>>> return v;
>>>}
>>>
>>>I can image you want some error-checking in there but there is not
>>>point in my guessing what or how you want to handle it.
>>>
>>
>> This will execute in the korn shell? (see subject line).
>
> No, obviously it's C.
>
> This is why it's a bad idea to put important information only in the
> subject line. It's easy to overlook it when concentrating on the body
> of the message to write a reply. Now Ben has wasted a bunch of time
> writing a function that doesn't help you, and you didn't get your
> question answered. Always make the message body self-contained, and the
> subject a summary of it.
>
> With regard to your question, I'm not aware of any slick way to do it.
> I think you just have to do it by hand, as you suspected.
Speaking of headers, I misread the attributions. Those comments should
be directed at the original poster, inetquestion@hotmail.com, and not at
Scott.
-
Re: ksh convert MB/GB to number
Nate Eldredge writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>
>> Ben Bacarisse writes:
>>>inetquestion writes:
>>>
>>>> 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?
>>>
>>>I'd use strtod. This will give you double and a pointer to first
>>>character that is not part of the number. I think this is safe even
>>>though EB looks like the start of an exponent (3.5E-2). Follow up
>>>with a switch based on the following character to determine the
>>>multiplier:
>>>
>>>double to_mb(const char *s)
>>>{
>>> char *ep;
>>> double v = strtod(s, &ep);
>>> if (ep > s) {
>>> switch (tolower((unsigned char)*ep)) {
>>> case 'b':
>>> v *= 1e-6;
>>> break;
>>> case 'k':
>>> v *= 1e-3;
>>> break;
>>> case 'g':
>>> v *= 1e3;
>>> break;
>>> case 't':
>>> v *= 1e6;
>>> break;
>>> case 'e':
>>> v *= 1e9;
>>> break;
>>> }
>>> }
>>> return v;
>>>}
>>>
>>>I can image you want some error-checking in there but there is not
>>>point in my guessing what or how you want to handle it.
>>>
>>
>> This will execute in the korn shell? (see subject line).
>
>No, obviously it's C.
>
>This is why it's a bad idea to put important information only in the
>subject line. It's easy to overlook it when concentrating on the body
>of the message to write a reply. Now Ben has wasted a bunch of time
>writing a function that doesn't help you, and you didn't get your
>question answered. Always make the message body self-contained, and the
>subject a summary of it.
>
FWIW, I wasn't the OP, and I had no problem understanding exactly
what the OP wanted. You should read the references headers better :-)
As for ksh:
a=6.54GB
if [[ "${a}" != "${a%%[gG][bB]}" ]];
then
echo "$(( ${a%%[gG][bB]} * 1024 )) Megabytes"
else if [[ "${a}" != "${a%%[Ee][bB]}" ]];
then
echo "$(( ${a%%[eE][bB]} * 1024 * 1024 * 1024 )) Megabytes"
else if ... and so forth.
fi
scott
-
Re: ksh convert MB/GB to number
On Oct 28, 6:24 pm, sc...@slp53.sl.home (Scott Lurndal) wrote:
> Nate Eldredge writes:
> >sc...@slp53.sl.home (Scott Lurndal) writes:
>
> >> Ben Bacarisse writes:
> >>>inetquestion writes:
>
> >>>> 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?
>
> >>>I'd use strtod. This will give you double and a pointer to first
> >>>character that is not part of the number. I think this is safe even
> >>>though EB looks like the start of an exponent (3.5E-2). Follow up
> >>>with a switch based on the following character to determine the
> >>>multiplier:
>
> >>>double to_mb(const char *s)
> >>>{
> >>> char *ep;
> >>> double v = strtod(s, &ep);
> >>> if (ep > s) {
> >>> switch (tolower((unsigned char)*ep)) {
> >>> case 'b':
> >>> v *= 1e-6;
> >>> break;
> >>> case 'k':
> >>> v *= 1e-3;
> >>> break;
> >>> case 'g':
> >>> v *= 1e3;
> >>> break;
> >>> case 't':
> >>> v *= 1e6;
> >>> break;
> >>> case 'e':
> >>> v *= 1e9;
> >>> break;
> >>> }
> >>> }
> >>> return v;
> >>>}
>
> >>>I can image you want some error-checking in there but there is not
> >>>point in my guessing what or how you want to handle it.
>
> >> This will execute in the korn shell? (see subject line).
>
> >No, obviously it's C.
>
> >This is why it's a bad idea to put important information only in the
> >subject line. It's easy to overlook it when concentrating on the body
> >of the message to write a reply. Now Ben has wasted a bunch of time
> >writing a function that doesn't help you, and you didn't get your
> >question answered. Always make the message body self-contained, and the
> >subject a summary of it.
>
> FWIW, I wasn't the OP, and I had no problem understanding exactly
> what the OP wanted. You should read the references headers better :-)
>
> As for ksh:
>
> a=6.54GB
>
> if [[ "${a}" != "${a%%[gG][bB]}" ]];
> then
> echo "$(( ${a%%[gG][bB]} * 1024 )) Megabytes"
> else if [[ "${a}" != "${a%%[Ee][bB]}" ]];
> then
> echo "$(( ${a%%[eE][bB]} * 1024 * 1024 * 1024 )) Megabytes"
> else if ... and so forth.
> fi
>
> scott
Scott/Ben - Thanks for the assistance!
solutions in ksh and C, what more could one ask for. 