Unix Shell - Grab decimal numbers and exponent

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Grab decimal numbers and exponent





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Grab decimal numbers and exponent
Samik R.

2006-10-23, 7:21 pm

Hello,

I am trying to grab numbers which can be decimals (2.3345) or exponents
(10.365E+03 or 110.1132E-10). I am using sed and doing the following for
decimals:
sed '/\([0-9]*.[0-9]*\)/\1/p' ...

which is catching the decimals. How do I modify this to catch the
exponential numbers as well?

Thanks.

-Samik
Xicheng Jia

2006-10-23, 7:21 pm

Samik R. wrote:
> Hello,
>
> I am trying to grab numbers which can be decimals (2.3345) or exponents
> (10.365E+03 or 110.1132E-10). I am using sed and doing the following for
> decimals:
> sed '/\([0-9]*.[0-9]*\)/\1/p' ...
>
> which is catching the decimals. How do I modify this to catch the
> exponential numbers as well?


try:

[0-9]*\.[0-9]*\([Ee][+-]\?[0-9]\+\)\?

Xicheng

Eric Moors

2006-10-24, 1:17 pm

Xicheng Jia wrote:

> Samik R. wrote:
>
> try:
>
> [0-9]*\.[0-9]*\([Ee][+-]\?[0-9]\+\)\?
>
> Xicheng


Beware though that you are missing out on valid numbers like 8 or 8E7.4
and are accepting numbers like 8.4.5.6E2

They may fall outside the expected input, but better safe than sorry.

eric
Xicheng Jia

2006-10-24, 1:17 pm

Eric Moors wrote:
> Xicheng Jia wrote:
>
>
> Beware though that you are missing out on valid numbers like 8 or 8E7.4
> and are accepting numbers like 8.4.5.6E2
> They may fall outside the expected input, but better safe than sorry.


Well, I just modified the OP's pattern based on the original
post/examples, if you do want to consider more cases, you can just make
some more adjustments, like from

[0-9]*\.[0-9]*

to

[0-9]*\.\?[0-9]\+

i.e.:

[0-9]*\.\?[0-9]\+\([Ee][+-]\?[0-9]*\.\?[0-9]\+\)\?

which will match 8 and 8E7.4 (you could certainly adjust it to match
8.E7)

If you can fix boundaries, the problem might be simpler. i.e.,
enclosing the pattern with

^ and $

or

\< and \>

or probably

\(^\|[^0-9.]\) and \([^0-9.]\|$\)

or

\(\<\|\.\) and \(\.\|\>\) (may match also '.7.e+.8')

(untested)

This really dependes on the context which the OP didnt mention. For
example, if all these numbers are stand-alone, the pattern could be:

^[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?$

or probably (no need to be stand-alone numbers but may miss the
leading/trailing 'dot'):

\<[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?\>

While I knew people here dont like complex stuff like these, I didnt
make my reply this way.. thank you very much for your understanding.

Regards,
Xicheng

Samik R.

2006-10-24, 1:17 pm

On 10/24/2006 9:19 AM, Xicheng Jia wrote:
> Eric Moors wrote:
>
> Well, I just modified the OP's pattern based on the original
> post/examples, if you do want to consider more cases, you can just make
> some more adjustments, like from
>
> [0-9]*\.[0-9]*
>
> to
>
> [0-9]*\.\?[0-9]\+
>
> i.e.:
>
> [0-9]*\.\?[0-9]\+\([Ee][+-]\?[0-9]*\.\?[0-9]\+\)\?
>
> which will match 8 and 8E7.4 (you could certainly adjust it to match
> 8.E7)
>
> If you can fix boundaries, the problem might be simpler. i.e.,
> enclosing the pattern with
>
> ^ and $
>
> or
>
> \< and \>
>
> or probably
>
> \(^\|[^0-9.]\) and \([^0-9.]\|$\)
>
> or
>
> \(\<\|\.\) and \(\.\|\>\) (may match also '.7.e+.8')
>
> (untested)
>
> This really dependes on the context which the OP didnt mention. For
> example, if all these numbers are stand-alone, the pattern could be:
>
> ^[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?$
>
> or probably (no need to be stand-alone numbers but may miss the
> leading/trailing 'dot'):
>
> \<[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?\>
>
> While I knew people here dont like complex stuff like these, I didnt
> make my reply this way.. thank you very much for your understanding.
>
> Regards,
> Xicheng
>

Thanks a lot for all the enlightening replies.
-Samik
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com