How to Convert Strings into Numbers under C-Shell?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to Convert Strings into Numbers under C-Shell?
# 1  
Old 01-16-2009
PHP How to Convert Strings into Numbers under C-Shell?

I tried something like:


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

but this doesn't seem to work. And then I tried:

@ test5 = $test3[1] - $test4[1]

This doesn't seem to work either.

Is it possible under C-shell to have test5 contain an array of the result test3 - test4 as numbers?
# 2  
Old 01-16-2009
shells, if they do any arithmetic at all, tend to do it with integers.
Look into bc.
# 3  
Old 01-16-2009
Quote:
Originally Posted by jpradley
shells, if they do any arithmetic at all, tend to do it with integers.
Look into bc.
Hi, if the shells have trouble handling floating point numbers, do you have any recommendation as to what I can use (Perl/awk/sed,etc) to obtain the numeric results I want?
# 4  
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
# 5  
Old 01-17-2009
Thank you very much for the detailed reply, but I seem to be a little lost here. Does c shell operate the same zsh does? I am not quite sure if my UNIX environment has zsh available. Otherwise the z-shell solution looks great for me!

So it appears that there's no easy way to process arithmetic for large data vectors directly if I am interpreting your message correctly. I was hoping there was something quick and light inside UNIX so I don't have to venture into Excel on the PC side, but I guess Excel is the dumb and effective way for me.

Is bc another tool like sed/awk/perl ? Can I use bc under c-shell?
# 6  
Old 01-17-2009
Hi.

Yes, bc, sed, perl, and awk are tools that may be used from any shell. The sed command will not do arithmetic.

A rule of thumb for me: a large volume of data would be files of size > 100 MB. One could use awk and perl, but they are not at their best for large quantities of data (perl uses double precision for arithmetic, which is fine for most uses, but slow for large volumes of data compared to c and Fortran). I would, however, use them unhesitatingly for anything smaller than that. I would find it inconvenient to fire up Windows & Excel for such a task.

I would not use the zsh built-ins for files above 1 MB unless it would be a single-shot use. To see if you have zsh, simply enter it as a command:
Code:
zsh

and you should get a prompt such as:
Code:
vm-lenny%

which was on Debian GNU/Linux.

Keep in mind that most modern computers are very fast.

This forum has responders that are particularly adept at awk, if you should need assistance.

What size are your typical data files?

Best wishes ... cheers, drl
# 7  
Old 01-17-2009
Hi.

Here is a short perl script that illustrates a convenience feature of perl -- including data with the script:
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate perl DATA, data included with script.

use warnings;
use strict;

my ($debug);
$debug = 0;
$debug = 1;

my ( $a, $b, $difference );

while (<DATA>) {
  ( $a, $b ) = split;
  $difference = $a - $b;
  print " difference $. for $a, $b is $difference\n";
  printf( " nicely formatted: %10.4e, %10.4e is %10.4e\n",
    $a, $b, $difference );
}

print STDERR " ( Lines read: $. )\n";

exit(0);

# Comments for comparison:
# 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)

__DATA__
4.985e-10 4.869e-10
5.130e-10 5.010-10
5.486e-10 5.363e-10
6.023e-10 5.895e-10
7.015e-10 6.887e-10

Producing:
Code:
% ./p1
 difference 1 for 4.985e-10, 4.869e-10 is 1.15999999999999e-11
 nicely formatted: 4.9850e-10, 4.8690e-10 is 1.1600e-11
Argument "5.010-10" isn't numeric in subtraction (-) at ./p1 line 16, <DATA> line 2.
 difference 2 for 5.130e-10, 5.010-10 is -5.009999999487
 nicely formatted: 5.1300e-10, 5.0100e+00 is -5.0100e+00
 difference 3 for 5.486e-10, 5.363e-10 is 1.23000000000001e-11
 nicely formatted: 5.4860e-10, 5.3630e-10 is 1.2300e-11
 difference 4 for 6.023e-10, 5.895e-10 is 1.28e-11
 nicely formatted: 6.0230e-10, 5.8950e-10 is 1.2800e-11
 difference 5 for 7.015e-10, 6.887e-10 is 1.28e-11
 nicely formatted: 7.0150e-10, 6.8870e-10 is 1.2800e-11
 ( Lines read: 5 )

Note that the error in your data was diagnosed, and that there are two methods of displaying the data: a quick one with print, and a more precise one with printf.

Most perl scripts are designed to read from disk files, but for small datasets, the DATA option can be useful.

Best wishes ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
Login or Register to Ask a Question