Real -> Integer (sed?)

Real -> Integer (sed?)

Post by Jan Boshof » Wed, 28 Jun 2000 04:00:00



Hi

I'm still having trouble converting between real and integers in unix
scripts.  I'm now using bash and have a shell script that calculates a
value using bc.  The result is something like 322.5000.

How can I convert this number to an integer, 322?

Can anyone suggest a sed command that will delete everything in a line
after and including the . ?

Thanks!

Jan

 
 
 

Real -> Integer (sed?)

Post by Friedrich Dominicu » Thu, 29 Jun 2000 04:00:00



> Hi

> I'm still having trouble converting between real and integers in unix
> scripts.  I'm now using bash and have a shell script that calculates a
> value using bc.  The result is something like 322.5000.

> How can I convert this number to an integer, 322?

> Can anyone suggest a sed command that will delete everything in a line
> after and including the . ?

example let t1.txt be
1123.00
1120320.00
111.20

then this works
sed -e 's/\([0-9]\+\)\.[0-9]\+/\1/g'  t1.txt

1123
1120320
111

Regards
Friedrich

--
for e-mail reply remove all after .com

 
 
 

Real -> Integer (sed?)

Post by t.. » Thu, 29 Jun 2000 04:00:00



> I'm still having trouble converting between real and integers in unix
> scripts.  I'm now using bash and have a shell script that calculates a
> value using bc.  The result is something like 322.5000.

> How can I convert this number to an integer, 322?

> Can anyone suggest a sed command that will delete everything in a line
> after and including the . ?

sed 's/\..*//'

However, shell builtin variable editing is probably better,
say if you have the result in variable X just do something like

Y=${X%.*}

The %.* construct will discard everything after and including
the last period in X, if any. (With two %'s it'd delete
everything from the _first_ period, same thing in this case.)

Of course you could also have bc truncate it with 'scale=0'
before final output, if you only need the integer part anyway.

--
Tapani Tarvainen

 
 
 

Real -> Integer (sed?)

Post by brian hile » Thu, 29 Jun 2000 04:00:00


Sed -- an abomination to use merely to discard the fractional part
of a floating point number.

${var%.*} -- a little better, but still not the best.

If you will experiment you will find that ksh (sorry, bash not tested)
automatically truncates variables set to a floating point numbers.

$ integer var=10.320
$ print -- $var
10

No muss, no fuss.

-Brian

 
 
 

Real -> Integer (sed?)

Post by David Hasset » Fri, 30 Jun 2000 04:00:00



> Sed -- an abomination to use merely to discard the fractional part
> of a floating point number.

> ${var%.*} -- a little better, but still not the best.

> If you will experiment you will find that ksh (sorry, bash not tested)

I did test bash. I got:

bash2.04$ typeset -i var=10.320
bash: declare: 10.320: syntax error in expression (error token is
".320")

Quote:> automatically truncates variables set to a floating point numbers.

> $ integer var=10.320
> $ print -- $var
> 10

> No muss, no fuss.

Nice. It is a pity bash doesn't like it really. As an aside, zsh does.
zsh$ echo $ZSH_VERSION
3.1.6-pws-13
zsh$ integer var=10.320
zsh$ echo $var
10

I thing I might begin to move away from my blind faith in bash. Ksh and
zsh do seem a lot less rusty in places. :-)

Cheers,

Dave. :-)

 
 
 

Real -> Integer (sed?)

Post by Peter Sundstro » Fri, 30 Jun 2000 04:00:00



>Hi

>I'm still having trouble converting between real and integers in unix
>scripts.  I'm now using bash and have a shell script that calculates a
>value using bc.  The result is something like 322.5000.

>How can I convert this number to an integer, 322?

>Can anyone suggest a sed command that will delete everything in a line
>after and including the . ?

Why use sed, when you can specify the number of decimal places with bc, eg:

bc <<EOF
scale=0
9/5
EOF

 
 
 

Real -> Integer (sed?)

Post by Chris F.A. Johns » Fri, 30 Jun 2000 04:00:00




: > Hi
: >
: > I'm still having trouble converting between real and integers in unix
: > scripts.  I'm now using bash and have a shell script that calculates a
: > value using bc.  The result is something like 322.5000.
: >
: > How can I convert this number to an integer, 322?

echo 322.5000 | cut -d. -f1

: > Can anyone suggest a sed command that will delete everything in a line
: > after and including the . ?

: example let t1.txt be
: 1123.00
: 1120320.00
: 111.20

: then this works
: sed -e 's/\([0-9]\+\)\.[0-9]\+/\1/g'  t1.txt

Or (much simpler):

        cut -d. -f1 t1.txt

: 1123
: 1120320
: 111

: Regards
: Friedrich

: --
: for e-mail reply remove all after .com
--
        Chris F.A. Johnson

        -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
               Everything in moderation - including moderation  

 
 
 

Real -> Integer (sed?)

Post by Friedrich Dominicu » Fri, 30 Jun 2000 04:00:00





> : > Hi
> : >
> : > I'm still having trouble converting between real and integers in unix
> : > scripts.  I'm now using bash and have a shell script that calculates a
> : > value using bc.  The result is something like 322.5000.
> : >
> : > How can I convert this number to an integer, 322?

> echo 322.5000 | cut -d. -f1

other have offered even better solutions ;-)

Quote:

> : > Can anyone suggest a sed command that will delete everything in a line
> : > after and including the . ?

I was just reading sed and so I don't thought further. 8-)

Regards
Friedrich

--
for e-mail reply remove all after .com

 
 
 

1. sed-users mailing list -> sed-users@egroups.com

If you use "sed" frequently then you may
wish to join the mailing list "sed-users".
Currently there are 150 members.

Free documentation is coming up on
sed.sourceforge.net - if you help us.

See my signature for more info..

Sven

--




2. Installing a diskless client - solaris 2.4

3. linux->real mode-> boot other OS in protected mode

4. libm problem on OSR505 UDK

5. SED Question << >>

6. Bash shell doesn't read profile

7. Using sed with \ and ' and <CR><LF>

8. Apache 1.3b3 problems.

9. Editting <> and </> from file using ed,sed, vi

10. Q: integer type with > 4 bytes

11. <><><> MOUNTING EXTENDED PARTITION <><><>

12. integer -> string