FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 02-18-2012, 05:54 PM
Johnny Rosenberg
 
Default Removing initial zero from variables

Here's yet another bash question, I know I asked quite a few lately:

I have a little problem regarding arithmetic expressions in bash.

I have a variable that contains a two digit number, for example 54 or
03. I want to use this variable in an arithmetic expression, something
like:
x=$((100*MyVar/MaxValue))
The problem ocurs when MyVar is less than 10, because 03, for example,
is considered an octal number. That works fine anyway in most cases,
but 08 and 09 does not, of course.

So I need some way to filter those initial zeroes out from MyVar:
01 → 1
02 → 2
and so on.
x=$(echo 08 | sed 's/^0*([^0]*)/1/')
echo $x
8
So far, so good.

However, 00 → 0 seems to be a problem:
x=$(echo 00 | sed 's/^0*([^0]*)/1/')
echo $x

(x is an empty string).

Maybe sed isn't the best tool for this, but what is? Is there a ”trim
number from inital zeroes” function available, perhaps?


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-18-2012, 06:05 PM
Johnny Rosenberg
 
Default Removing initial zero from variables

Den 18 februari 2012 19:54 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
> Here's yet another bash question, I know I asked quite a few lately:
>
> I have a little problem regarding arithmetic expressions in bash.
>
> I have a variable that contains a two digit number, for example 54 or
> 03. I want to use this variable in an arithmetic expression, something
> like:
> x=$((100*MyVar/MaxValue))
> The problem ocurs when MyVar is less than 10, because 03, for example,
> is considered an octal number. That works fine anyway in most cases,
> but 08 and 09 does not, of course.
>
> So I need some way to filter those initial zeroes out from MyVar:
> 01 → 1
> 02 → 2
> and so on.
> x=$(echo 08 | sed 's/^0*([^0]*)/1/')
> echo $x
> 8
> So far, so good.
>
> However, 00 → 0 seems to be a problem:
> x=$(echo 00 | sed 's/^0*([^0]*)/1/')
> echo $x
>
> (x is an empty string).
>
> Maybe sed isn't the best tool for this, but what is? Is there a ”trim
> number from inital zeroes” function available, perhaps?
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・*ーゼンバーグ

Found it, I think:
$ echo 00 | bc
0
$ echo 000008 | bc
8
$

I'm not sure if there are any drawbacks yet, though…


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-18-2012, 06:20 PM
Johnny Rosenberg
 
Default Removing initial zero from variables

Den 18 februari 2012 20:05 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
> Den 18 februari 2012 19:54 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
>> Here's yet another bash question, I know I asked quite a few lately:
>>
>> I have a little problem regarding arithmetic expressions in bash.
>>
>> I have a variable that contains a two digit number, for example 54 or
>> 03. I want to use this variable in an arithmetic expression, something
>> like:
>> x=$((100*MyVar/MaxValue))
>> The problem ocurs when MyVar is less than 10, because 03, for example,
>> is considered an octal number. That works fine anyway in most cases,
>> but 08 and 09 does not, of course.
>>
>> So I need some way to filter those initial zeroes out from MyVar:
>> 01 → 1
>> 02 → 2
>> and so on.
>> x=$(echo 08 | sed 's/^0*([^0]*)/1/')
>> echo $x
>> 8
>> So far, so good.
>>
>> However, 00 → 0 seems to be a problem:
>> x=$(echo 00 | sed 's/^0*([^0]*)/1/')
>> echo $x
>>
>> (x is an empty string).
>>
>> Maybe sed isn't the best tool for this, but what is? Is there a ”trim
>> number from inital zeroes” function available, perhaps?
>>
>>
>> Kind regards
>>
>> Johnny Rosenberg
>> ジョニー・*ーゼンバーグ
>
> Found it, I think:
> $ echo 00 | bc
> 0
> $ echo 000008 | bc
> 8
> $
>
> I'm not sure if there are any drawbacks yet, though…
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・*ーゼンバーグ

Oh… and here is a better one:
~$ MyVar=0008
~$ x=$((100*$[10#$MyVar]/12))
~$ echo $x
66
~$

”$[10#$MyVar]” forces bash to use decimal representation.


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
I�Ey�|���^���aj�!���[��ƫrb��'������{�j�!� ޖ+ޯ'Z�䚷_y����۞z��z+��� W^j�!�)����x���=o+6��ڶ*l� �0���ki���&#x1E5897;���� �zw�jV���܆X��+�'uG*n�M4� ��{�j�!����_y����۞z� z+������{��� ����܆X��+������o(��]y�܆X��+���a#>'@�M;��^om5�6 ��I4^q���h��]y�܆X��+���W^j�!�)����x �������{������"� 13�C�v����r���܆X��+�& ��^om5�6��I4^q�ԏ��� ��W(���� ��m=��"������m���+� � ��W(�)�&�ק�+r��x��*x�|� {����Ǣ��
 

Thread Tools




All times are GMT. The time now is 05:25 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org