Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to Convert Strings into Numbers under C-Shell? Post 302277686 by drl on Saturday 17th of January 2009 01:22:03 PM
Old 01-17-2009
Hi.

Here are two options, one in a shell, the other in bc:
Code:
#!/usr/bin/env zsh

# @(#) s1       Demonstrate floating point arithmetic, zsh, bc.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) bc
set -o nounset

# set test3 = (4.985e-10 5.130e-10 5.486e-10 6.023e-10 7.015e-10)
# set test4 = (4.869e-10 5.010-10 5.363e-10 5.895e-10 6.887e-10)
# set test5 = $test3 - $test4

float difference test3 test4
echo
echo " Results of zsh built-in floating-point arithmetic:"
test3=(4.985e-10 5.130e-10 5.486e-10 6.023e-10 7.015e-10)
echo " test3 is $test3"
echo " test3[0] is ${test3[0]}"
echo " test3[1] is ${test3[1]}"
test4=(4.869e-10 5.010-10 5.363e-10 5.895e-10 6.887e-10)
echo " test4 is $test4"
echo " test4[2] is ${test4[2]}"

echo
for ((i=1 ; i<=${#test3} ; i++ ))
do
  (( difference = ${test3[$i]}-${test4[$i]} ))
  printf " difference $i of ${test3[$i]} ${test4[$i]} is %e\n" $difference
done

echo
echo " Results from bc, \"e\" format, expect error:"
t3=${test3[1]}
t4=${test4[1]}
echo "scale=4 ; $t3 - $t4" | tee t1 | bc
echo " input was:"
cat t1

echo
echo " Results from bc, format of bc:"
t3="4.985*10^-10"
t4="4.869*10^-10"
echo "scale=15 ; $t3 - $t4" | tee t1 | bc
echo " input was:"
cat t1

exit 0

Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
zsh 4.2.4
bc 1.06

 Results of zsh built-in floating-point arithmetic:
 test3 is 4.985e-10 5.130e-10 5.486e-10 6.023e-10 7.015e-10
 test3[0] is 4.985e-10
 test3[1] is 4.985e-10
 test4 is 4.869e-10 5.010-10 5.363e-10 5.895e-10 6.887e-10
 test4[2] is 5.010-10

 difference 1 of 4.985e-10 4.869e-10 is 1.160000e-11
 difference 2 of 5.130e-10 5.010-10 is -1.501000e+01
 difference 3 of 5.486e-10 5.363e-10 is 1.230000e-11
 difference 4 of 6.023e-10 5.895e-10 is 1.280000e-11
 difference 5 of 7.015e-10 6.887e-10 is 1.280000e-11

 Results from bc, "e" format, expect error:
(standard_in) 1: parse error
(standard_in) 1: parse error
 input was:
scale=4 ; 4.985e-10 - 4.869e-10

 Results from bc, format of bc:
.000000000011600
 input was:
scale=15 ; 4.985*10^-10 - 4.869*10^-10

Difference 2 seems odd at first until you look closely at your input data:
Quote:
Originally Posted by EDALBNUG
set test3 = (4.985e-10 5.130e-10 5.486e-10 6.023e-10 7.015e-10)
set test4 = (4.869e-10 5.010-10 5.363e-10 5.895e-10 6.887e-10)
The bc format is somewhat clumsy. Other options include awk and perl, but not for production quantity volumes of data. There is the statistics package "S" (in Linux "R") which might be useful, and may do vectors, in which it looks like you are interested. If I get some time, I may post something about that. For large quantities of numbers, C and Fortran would be most useful.

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Req on how to convert hex numbers to decimals

Hi, If i have an input as c1:41 c2:0x0000.00046b3e I want to make output display as c1:41 c2:224062 . Basically convert first part 0x0000 (as hex) to decimal which is 0 and convert second part 0x00046b3e (as hex) to decimal which is 289598 and as such add both parts namely... (3 Replies)
Discussion started by: hare
3 Replies

2. Shell Programming and Scripting

extracting numbers from strings

Hello all, I am being dumb with this and I know there is a simple solution. I have a file with the follwing lines bc stuff (more)...............123 bc stuffagain (moretoo)............0 bc stuffyetagain (morehere)......34 failed L3 thing..............1 failed this... (2 Replies)
Discussion started by: gobi
2 Replies

3. Shell Programming and Scripting

How to convert hex numbers to decimal ?

Hi, please tell me how to convert hex number to decimal 000000E7 000000000002640D 0000000000025B16 and seconds to minutes, hours, days, months, years bytes to kbytes, mbytes , gbytes read the following examples while read a b do printf "%5d %5d\n" "0x$a" "0x$b" done < "$FILE"... (15 Replies)
Discussion started by: jack2
15 Replies

4. Programming

C++ Formatting Numbers to Strings

Hi All, Sorry to say I have 0 experience writing C++ but have been asked to write a piece of code that will take a double input and an integer for number of decimal places as well as integer for padding and output a string that represents the double formatted (with comma thousand separators -... (2 Replies)
Discussion started by: Leedor
2 Replies

5. UNIX for Dummies Questions & Answers

Finding numbers in lines with strings and number and doing some manipulation

Hi, I want to write a script that does something like this: I have a file, in which in every line, there is a string of words, and followed by some space, a number. Now, I want to identify the line, which has the largest startFace number (say m=8118), take that number and add it to the... (2 Replies)
Discussion started by: super_commando
2 Replies

6. Shell Programming and Scripting

Replace a multi-line strings or numbers

Hi I have no experience in Unix so any help would be appreciated I have the flowing text 235543 123 45654 199 225 578 45654 199 225 I need to find this sequence from A file 45654 199 225 (22 Replies)
Discussion started by: khaled79
22 Replies

7. Shell Programming and Scripting

Convert Columns in Rows delimited by a strings

Hi Gurus, I have a file that contain inventory information from someones computers: UserName domain\user1 DNSHostName machine1 Caption Microsoft Windows 7 Professional OSArchitecture 64 bits SerialNumber XXX Name HP EliteBook Revolve 810 G1 NumberOfProcessors 1 Name Intel(R)... (2 Replies)
Discussion started by: gilmore666
2 Replies

8. Shell Programming and Scripting

Sort strings with numbers

I want to sort my data first by the 2nd field then by the first field. I can't use sort -V because I don't have gnu sort and cannot install one. How do I go about this? Input: G456 KT1 34 K234 KT10 45 L2 KT2 26 H5 LAF2 28 F3 LAF2 36 Output: G456 KT1 34 L2 KT2 26 K234 KT10 45 F3... (14 Replies)
Discussion started by: aydj
14 Replies

9. Shell Programming and Scripting

Sort strings containing numbers

How can I sort this, first by 2nd field then by 1st field. tried sort -b -k 2,2 Input: AS11 AB1 BD34 AB10 AF12 AC2 A345 AB10 R134 AB2 456 AC10 TTT2 BD12 desired output: AS11 AB1 R134 AB2 A345 AB10 BD34 AB10 AF12 AC2 456 AC10 TTT2 BD12 (2 Replies)
Discussion started by: aydj
2 Replies

10. Shell Programming and Scripting

Convert list of numbers to text range

Hi, I'd like to take a list of numbers (with a prefix) and convert to a range, for example: cn001 cn004 cn016 cn017 cn018 cn019 cn020 cn021 cn031 cn032 cn038 cn042 cn043 cn044 cn045 (5 Replies)
Discussion started by: chrissycc
5 Replies
echo(3XCURSES)						  X/Open Curses Library Functions					    echo(3XCURSES)

NAME
echo, noecho - enable/disable terminal echo SYNOPSIS
cc [ flag... ] file... -I /usr/xpg4/include -L /usr/xpg4/lib -R /usr/xpg4/lib -lcurses [ library... ] c89 [ flag... ] file... -lcurses [ library... ] #include <curses.h> int echo(void); int noecho(void); DESCRIPTION
The echo() function enables Echo mode for the current screen. The noecho() function disables Echo mode for the current screen. Initially, curses software echo mode is enabled and hardware echo mode of the tty driver is disabled. The echo() and noecho() functions control soft- ware echo only. Hardware echo must remain disabled for the duration of the application, else the behavior is undefined. RETURN VALUES
Upon successful completion, these functions return OK. Otherwise, they return ERR. ERRORS
No errors are defined. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
getch(3XCURSES), getstr(3XCURSES), initscr(3XCURSES), libcurses(3XCURSES), scanw(3XCURSES), attributes(5), standards(5) SunOS 5.10 5 Jun 2002 echo(3XCURSES)
All times are GMT -4. The time now is 07:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy