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?
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Here are two options, one in a shell, the other in bc:
Producing:
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.
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?
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
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:
and you should get a prompt such as:
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.
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Here is a short perl script that illustrates a convenience feature of perl -- including data with the script:
Producing:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)