Sponsored Content
Top Forums Shell Programming and Scripting Floating point to integer in variable length lines Post 302413276 by jossojjos on Thursday 15th of April 2010 05:45:00 AM
Old 04-15-2010
Floating point to integer in variable length lines

Hi !

I'm looking for a way to transform certain floating point numbers in a one-line, variable length file to integers.

I can do this in a crude way with sed :
Code:
sed -e 's/0\.\([1-9][0-9]\)[0-9]:/\1:/g' -e 's/0\.0\([0-9]\)[0-9]:/\1:/g' -e 's/1\.000:/100:/g' myfile

... but this doesn't handle the rounding correctly.

I'm not very familiar with awk or perl, which are more adapted to arithmetics than sed.
So I can use them to look for my number to replace, but how to get the arithmetics done ?

So far, I can recognize the numbers to change :
Code:
awk  '{gsub(/)[0-9]+\.[0-9]+:/,")boot:")};1' myfile

or :
Code:
perl -pe 's/\)([0-9]+\.[0-9]+):/\)$1*100:/g' myfile

The floating point number to replace is always between a ")" and a ":" ; other numbers should be left unchanged.
So in the awk command, can I replace "boot" with an arithmetic operation ?
In the perl command, how to do the arithmetics and not just printing "0.100*100" ?

Just to give an example of the file, but they can be much more complicated :
Code:
(X1.13:0.39456,X1:0.00014,(Y3:0.01259,(XX:0.18846,YK3:0.14006)1.000:0.73816)0.758:0.01603);

The numbers to be changed are "1.000" and "0.758".

Thanks for reading !
jos
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

floating point variable in UNIX script

HI all, Can someone help me how to use or assign a floating point value in a variable in a unix script. I dont know how to use it. for example, I am to assign a variable VAR1 a value of 2.34 and evaluate if a certain vaue is greater or less than to it. That is, if var1 > 2.13 will do... (7 Replies)
Discussion started by: darry
7 Replies

2. Shell Programming and Scripting

floating point shell variable

I have a two files >cat file1 jjjjj 10.345 6.673 ppp 9.000 5.883 >cat file2 mmm 80 10 jjjjj 10.305 6.873 ppp 9.000 5.883 I am reading file 1 line by line , and look for the string jjjj and then read the line in file 2 with jjjj I want to get the... (5 Replies)
Discussion started by: jojan
5 Replies

3. Shell Programming and Scripting

Replace floating-point by integer in awk

Hi, I am trying to write a script to extract multiple sets of data from a chemistry output file. The problem section is in the following format... Geometry "geometry" -> "geometry" 1 Pd 46.0000 -0.19290971 0.00535260 0.02297606 2 P ... (7 Replies)
Discussion started by: smadonald1
7 Replies

4. Shell Programming and Scripting

Rounding off the value of Floating point value

Hello, i have some variables say: x=1.4 y=3.7 I wish to round off these values to : x = 2 (after rounding off) y = 4 (after rounding off) I am stuck. Please help. (7 Replies)
Discussion started by: damansingh
7 Replies

5. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

6. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies

7. UNIX for Dummies Questions & Answers

Check if input is an integer or a floating point?

Hiii I actually intent to check the integer or floating point number input by user i.e. 23, 100, 55.25, 12.50 ..etc. However, when someone input strings or alpha character, my program has to show invalid input.!! Is there any Unix shell script syntax can help me to check ? Thanking you (2 Replies)
Discussion started by: krishnampkkm
2 Replies

8. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

9. Shell Programming and Scripting

Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers. The length of the number should be 13 including decimal. The input number is changing so number of leading zeros is not fix. For example input output 216.000 000000216.000 1345.000 000001345.000 22345.500 ... (4 Replies)
Discussion started by: reeta_shri
4 Replies

10. Shell Programming and Scripting

IF statement on floating point variable in scientific format

Hi all, I'm struggling :wall: to make a an if statement on two variables in scientific format. e.g. a=1.23e+01 b=3.21e+02 I know that it can be done with bc if the variable are not in scientific format. But what to do in this case??? Thank you, (3 Replies)
Discussion started by: f_o_555
3 Replies
nextafterd64(3M)														  nextafterd64(3M)

NAME
nextafterd64(), nextafterd128(), nextafterd32(), nexttowardd64(), nexttowardd128(), nexttowardd32() - next representable decimal floating- point numbers SYNOPSIS
DESCRIPTION
The function computes the next representable number following x in the direction toward y. Thus, if y is less than x, returns the largest representable number less than x. The function returns y if x equals y. The function is equivalent to except that the second parameter has the widest decimal floating-point type, and the function returns y con- verted to the type of the function if x equals y. The result of the function is determined in the type of the function, without loss of range or precision in a floating second argument, whose type may be wider than the type of the function. is a version of it takes arguments and returns a result. is a version of it takes arguments and returns a result. is a version of it takes a first argument and returns a result. is a version of it takes a first argument and returns a result. These math functions are supported for HP-UX 11i Version 3 September 2008 Update and forward. USAGE
To use any of these functions, compile with the option. Make sure your program defines and then includes Link in the math library by specifying or on the linking command line. For more information, see the at the following site: RETURN VALUE
If x equals y, returns y, and returns y converted to the type of x. If x is finite and the correct function value would overflow, these functions return according to the sign of x, and raise the overflow and inexact floating-point exceptions. If the return value is subnormal and x!=y, these functions raise the underflow and inexact floating-point exceptions. If x or y is NaN, these functions return NaN. ERRORS
If x is finite and the correct function value would overflow, these functions set to SEE ALSO
scalbnd64(3M), math(5). STANDARDS CONFORMANCE
These functions conform to ISO/IEC TR 24732, "Extension for the programming language C to support decimal floating-point arithmetic". HP Integrity Server Only nextafterd64(3M)
All times are GMT -4. The time now is 03:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy