How to convert a string into double in UNIX

I have a file where i have the start and end timings of jobs. (Odd line will have start and even will have end)

Say File "X.txt" has two lines as below

1333553619.647
1333553620.859

I have used the following bit of code, to fetch the values from the file.

even=`sed -n '2p' X.txt'`
odd=`sed -n '1p' X.txt'`
typeset -E odd
typeset -E even

echo of odd and even gives me 1333553619 and 1333553620 respectively. So the difference calculated will be 1.

But i need the 3 digits in the decimal place also for exact difference calculations.

So what needs to be done ?

Pl help me reg this. Bit urgent :frowning:

% cat infile
1333553619.647
1333553620.859
% sed 'N; s/\(.*\)\n\(.*\)/\2-\1/' infile|bc
1.212
1 Like

You can try (in ksh93):

typeset -lE
1 Like

Thanks dude :slight_smile:

---------- Post updated 04-09-12 at 10:07 AM ---------- Previous update was 04-08-12 at 10:27 AM ----------

One more problem. (Note: I dont have ksh93, working on ksh88)

%cat infile
1333553619.647
1333553620.859
1333553622.246
1333553623.470
1333553624.993
1333553627.028
1333553628.925
1333553629.531
1333553630.663
1333553630.684

I need to find out the difference of even and odd values. Say Value 2 - Value 1, Value 4 - Value 3, ... Value n - Value (n-1).

I have tried to check whether the code given by radoulov works for values 4 and 3 using

%sed 'N; s/\(.*\)\n\(.*\)/\4 - \3/' infile | bc

But it gave the error,

sed: Function N; s/\(.*\)\n\(.*\)/\4 - \3/ cannot be parsed.

I'm a newbie to UNIX. Pl help.

in awk,

{
if NR % 2 == 0
 print $1-ODD
else
 ODD=$1
}

Please try.

OK

1 Like

@kalidas
I don't understand. Running Radoulov's code:

$ sed 'N; s/\(.*\)\n\(.*\)/\2 - \1/' infile | bc
1.212
1.224
2.035
.606
.021

Why is this not working for you?

1 Like

I have changed the code to

$ sed 'N; s/\(.*\)\n\(.*\)/\4 - \3/' infile | bc

and tried. That's why it throwed the error.

Small misunderstanding in the way of usage of code. It is working perfectly :slight_smile:

Thanks Scrutinizer and Radoulov.

awk:

awk 'getline p{print $1-p}' infile