It is currently Mon, 17 Feb 2020 19:33:30 GMT



 
Author Message
 deleteing unwanted zero's after the dec point
Hello,

I am tryin gto use sed to delete unwanteded zero's, and I need help.

I have a series of fixed length numbers like this;.;
250.000000
211.000000
113.000000
..
Waht I want to do is strip off those zero's after the decimal point, so
I am left with;.;
250
211
113

I have tried this
num="250.000000"
echo $num | sed  "s/\......//g"
Using the decimal point, and then using the 'dot' again to count out
six characters to delete them. But this does not work.

Help.

Jake.

Sent via Deja.com http://www.**-**.com/
Before you buy.



 Fri, 31 May 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point

If you only want what's before the decimal point (which it seems you do),
try awk:  
  awk -F. '{print $1}'

If you want to use sed to strip off decimal pt and beyond, try:
  sed 's/\..*//g'

If you want to remove trailing 0's, and the decimal pt ONLY if there
are no non-zero digits after it, try:
  sed 's/0*$//;s/\.$//'

$ echo 211.1000000 |sed 's/0*$//;s/\.$//'
211.1
$ echo 211.000000 |sed 's/0*$//;s/\.$//'
211
$ echo 211.0010000 |sed 's/0*$//;s/\.$//'
211.001

 - Matt

--
 Matthew Landt <la...@austin.ibm.com>   _    _  .  _ __.____.  \/\|/\/
  AIX and HACMP Certified Specialist   | |  / \ |\| |  \. ,_|  ` o O '
  / Comments, views, and opinions \    | |_/ ^ \|   | ) | |       x    
  \ are mine alone, not IBM's.    /    |___|/~\_\_|\|__/|_|     \___/



 Fri, 31 May 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point

Jake,

This looks seriously like homework!

For the solution that you have above you miscounted on your number of
zeros.  Try adding another "." and it will work.

Will also tell you that there are LOADS of different methods for
handling this problem.

You could use:

sed
awk
cut

just to name a few (with several variations on each).

Depends on your requirements ... are you always going to want integers?
always the same number of zeros?

Good luck,
Ted Loeffelholz
Caterpillar Inc



 Fri, 31 May 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point

If all the poster's nubmers are ALWAYS XYZ.000000, this code will work.
However, if the number might change a little, this code breaks down.
Even if the numbers are in the above format, I would try to code for
other posibilities, so future enhancements don't break.

Here are some numbers that show the breakdown:

$ echo 250.0 |sed  "s/\.00*//g"
250
# Valid return.

$ echo 250. |sed  "s/\.00*//g"
250.
# Bad return. Still has decimal point.

$ echo 250.010 |sed  "s/\.00*//g"
25010
# Bad return.  250.01 turns into 25010 (factor of 100) VERY BAD!
# Just imagine if the orig number was 2.000001, 2 -> 2 Million!

$ echo 250.1000 |sed  "s/\.00*//g"
250.1000
# Bad return, trailing 0's NOT removed.

 - Matt

--
 Matthew Landt <la...@austin.ibm.com>   _    _  .  _ __.____.  \/\|/\/
  AIX and HACMP Certified Specialist   | |  / \ |\| |  \. ,_|  ` o O '
  / Comments, views, and opinions \    | |_/ ^ \|   | ) | |       x    
  \ are mine alone, not IBM's.    /    |___|/~\_\_|\|__/|_|     \___/



 Fri, 31 May 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point

 $ echo 2110 |sed 's/0*$//;s/\.$//'      
 211

Bad guess.

Try

    sed 's/\(\.[0-9]*[1-9]\)0*$/\1/;s/\.0*$//'

instead as follows:

 $ echo 211.1000 |sed 's/\(\.[0-9]*[1-9]\)0*$/\1/;s/\.0*$//'
 211.1
 $ echo 211.0000 |sed 's/\(\.[0-9]*[1-9]\)0*$/\1/;s/\.0*$//'
 211
 $ echo 211.0010 |sed 's/\(\.[0-9]*[1-9]\)0*$/\1/;s/\.0*$//'
 211.001
 $ echo 21100 |sed 's/\(\.[0-9]*[1-9]\)0*$/\1/;s/\.0*$//'    
 21100

 .m.

--
Juergen Ernst Guenther
Widersacher der seelenlosen Lakaien der Orthodoxie



 Sat, 01 Jun 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point
In article <8334e6$a9...@nnrp1.deja.com>,

Assuming you just need to strip off the decimals including the decimal
point: If you are using ksh you can avoid sed, awk, and the lot by
using the shells built in functionality for this. Assuming $num holds
the number:

print ${num%.*}

Or if you read the numbers from a file foo:

while read num; do
  print ${num%.*}
done < foo

This works in bash as well, though bash has a built in echo command
instead of ksh's print.

/Peter
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Before you buy.



 Sat, 01 Jun 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point

Ahhh!!  Geez, I hate it when I miss one.  Thanks for the correction!

 - Matt
--
 Matthew Landt <la...@austin.ibm.com>   _    _  .  _ __.____.  \/\|/\/
  AIX and HACMP Certified Specialist   | |  / \ |\| |  \. ,_|  ` o O '
  / Comments, views, and opinions \    | |_/ ^ \|   | ) | |       x    
  \ are mine alone, not IBM's.    /    |___|/~\_\_|\|__/|_|     \___/



 Sat, 01 Jun 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point
It was: Tue, 14 Dec 1999 13:33:57 GMT  and with STARTLING insight,  "PEZ
<pe...@my-deja.com>"
  posted "Re: deleteing unwanted zero's after the dec point"
 to "comp.unix.shell" :

-->In article <8334e6$a9...@nnrp1.deja.com>,
-->> I am tryin gto use sed to delete unwanteded zero's, and I need help.
-->> I have a series of fixed length numbers like this;.;
-->> 250.000000
-->> 211.000000
-->> 113.000000
-->> Waht I want to do is strip off those zero's after the decimal point,
-->so
-->> I am left with;.;
-->> 250
-->> 211
-->> 113

-->Assuming you just need to strip off the decimals including the decimal
-->point: If you are using ksh you can avoid sed, awk, and the lot by
-->using the shells built in functionality for this. Assuming $num holds
-->the number:

-->print ${num%.*}

-->Or if you read the numbers from a file foo:

-->while read num; do
-->  print ${num%.*}
-->done < foo
-->
-->This works in bash as well, though bash has a built in echo command
-->instead of ksh's print.
-->
-->/Peter

This works in bash with the printf (builtin) command too:

num=333.4444

printf ${num%.*}
333

printf ${num%%.*}
333

Not sure what you're doing exactly here, nor why both forms work the same
-- there must be a difference in some cases;  the man file for bash shows,
in somewhat inscrutable form:

${parameter%word}
${parameter%%word}
 The  word  is  expanded to produce a pattern just as in pathname
expansion.
 If the pattern matches a trailing portion of the value of  parameter,
then
 the  expansion is the value of parameter with the shortest matching
pattern
 deleted (the ``%'' case) or  the  longest  matching  pattern  deleted
(the ``%%'' case).

/ts

              tenox  @  home  dot   com



 Mon, 03 Jun 2002 03:00:00 GMT   
 deleteing unwanted zero's after the dec point

If there is only one . in the string, there is no difference.
But consider:
  $ foo=333.4444.55555
  $ printf ${foo%.*}\\n
  333.4444
  $ printf ${foo%%.*}\\n
  333

                --Ken Pizzini



 Tue, 04 Jun 2002 03:00:00 GMT   
 
   [ 9 post ] 

Similar Threads

1. deleteing '\015' from multiple files

2. Floppy reports size as zero sectors, zero tracks, zero bytes

3. how do you point a file at a zero inode

4. tunnel point to point vs physical point to point

5. Point-to-Point-to-Point-to-Point ad nauseum

6. Point-to-Point-to-Point

7. select() returns on unwanted fd's

8. 'floating point exception' when running top

9. smbmount 'mount point'

10. MS relieves patching 'pain point'


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software