How to convert a string into double in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert a string into double in UNIX
# 1  
Old 04-07-2012
Data 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
Code:
1333553619.647
1333553620.859

I have used the following bit of code, to fetch the values from the file.
Code:
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 Smilie

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 04-07-2012 at 08:22 AM..
# 2  
Old 04-07-2012
Code:
% cat infile
1333553619.647
1333553620.859
% sed 'N; s/\(.*\)\n\(.*\)/\2-\1/' infile|bc
1.212

This User Gave Thanks to radoulov For This Post:
# 3  
Old 04-07-2012
You can try (in ksh93):
Code:
typeset -lE

This User Gave Thanks to yazu For This Post:
# 4  
Old 04-09-2012
Thanks dude Smilie

---------- 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)


Code:
%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
Code:
%sed 'N; s/\(.*\)\n\(.*\)/\4 - \3/' infile | bc



But it gave the error,
Code:
sed: Function N; s/\(.*\)\n\(.*\)/\4 - \3/ cannot be parsed.


I'm a newbie to UNIX. Pl help.

Last edited by Scrutinizer; 04-09-2012 at 04:16 AM.. Reason: some extra code tags
# 5  
Old 04-09-2012
in awk,
Code:
{
if NR % 2 == 0
 print $1-ODD
else
 ODD=$1
}

Please try.

OK

Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-09-2012 at 05:43 AM.. Reason: Code tags
This User Gave Thanks to ananthap For This Post:
# 6  
Old 04-09-2012
@kalidas
I don't understand. Running Radoulov's code:

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

Why is this not working for you?
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-09-2012
Bug Radoulov's code is working fine :)

Quote:
Originally Posted by Scrutinizer
@kalidas
I don't understand. Running Radoulov's code:

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

Why is this not working for you?
I have changed the code to
Code:
$ 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 Smilie

Thanks Scrutinizer and Radoulov.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

2. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

Convert csv to pipe delimited except the ones in double quotes

I have a csv data file : A,B,C,D,"A,B",E,"GG,H" E,F,G,H,I,J,"S,P" I need to replace all "," with "|" except the ones between double quotes i.e A|B|C|D|"A,B"|E|"GG,H" E|F|G|H|I|J|"S,P" CAn someone assist? (8 Replies)
Discussion started by: Shivdatta
8 Replies

4. Shell Programming and Scripting

Help with convert string

Hi. I will be very appreciated for help. I need replace all characters into string with \ (backslash) I mean if I have word abcdefg as input. How I can convert it to \a\b\c\d\e\f\g Thanks and best regards. Staas. (5 Replies)
Discussion started by: beckss
5 Replies

5. Shell Programming and Scripting

Convert string in a column

Hi I got this: aix.acct,aix.system.config.cron,aix.system.config.src,aix.system.install,string2 and I want this: aix.acct system.config.cron aix.system.config.src aix.system.install string2 I tried using sed by changing ',' into an 'ENTER' but I couldn't. :-( thxs (2 Replies)
Discussion started by: iga3725
2 Replies

6. Shell Programming and Scripting

Convert output to string

Hi Guys, is there any command to convert the output returned by the below command to string format: Code: sed 1!d filename Output is : 108 ---------- Post updated at 11:03 AM ---------- Previous update was at 11:00 AM ---------- Because i am using this output as string parameter ... (4 Replies)
Discussion started by: kam786sim
4 Replies

7. Shell Programming and Scripting

To convert a date(in string format) to unix timestamp

Hi All, I have a string like below. "Mar 31 2009" . I want to convert this to unix time . Also please let me know how to find the unix time for the above string minus one day. For Eg. if i have string "Mar 31 2009" i want to find the unix time stamp of "Mar 30 2009". Thanks in advance,... (11 Replies)
Discussion started by: girish.raos
11 Replies

8. Shell Programming and Scripting

how to convert array into the string

Hi I have a line/string as follows: A=" 3498 NORDEA - INDX LINKED NORY" which was converted into an array of characters: p321$ echo "${ARR}" 3 4 9 8 N O R D E A - I N D X L I N K E D N O R Y When I am trying print this array there are blank... (4 Replies)
Discussion started by: aoussenko
4 Replies

9. Shell Programming and Scripting

Convert String to Date Unix

Hi people, I need to convert a string eg 09/13/2008 to a valid unix date. (4 Replies)
Discussion started by: sameerspice
4 Replies

10. Shell Programming and Scripting

Convert String to Date

Hi, I have a String input parameter like this: 20080430 (YYYYMMDD). Inside my korn shell script I need to add one day to this date. L_TRADE_DAY=$1 let L_TODAY=$L_TRADE_DAY+1 Offcourse this raises a problem at the end of a month. 20080430 + 1 gives 20080431 instead of 20080501. ... (2 Replies)
Discussion started by: ORatjeuh
2 Replies
Login or Register to Ask a Question