Computing.Net > Forums > Unix > arithmetic using awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

arithmetic using awk

Reply to Message Icon

Name: trasver
Date: February 21, 2006 at 01:19:38 Pacific
OS: Unix
CPU/Ram: G5, 2GB
Product: apple
Comment:

Hi there

I'm new to performing calculations using awk or some
such tool withi a UNIX shell script. I have a formula which
I need to calculate. I've done it using bc but it involves
breaking down the formula and performing 8 calculations
which I'm sure isn't the best way to do this! There are
input values to the formula which I have assigned to A, B,
C, D, E and F in the script.

The formula is as follows:

Square root of (A-D)^2 + (B-E)^2 + (C-F)^2

where (A-D)^2 equals A-D squared.

BTW all or some of A-F may have negative values!

It doesn't necessarily have to be performed using awk but
the faster the better as I'll have a lot of calculations to do!

Thanks for any assistance.

Simon



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 21, 2006 at 16:24:58 Pacific
Reply:

Here is an awk solution, but you might be better off embeeding your bc solution in a script. I think bc probably has a smaller foot print than awk:

#!/bin/ksh

A=63
D=-1
B=51
E=2
C=89
F=8

# should be 24
echo ""|awk '{ print sqrt(A-D)+sqrt(B-E)+sqrt(C-F) }' A=$A D=$D B=$B E=$E C=$C F=$F


0
Reply to Message Icon

Related Posts

See More


Awk duplicate record elim... passing value between uni...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: arithmetic using awk

Using AWK www.computing.net/answers/unix/using-awk/6583.html

profiling using awk www.computing.net/answers/unix/profiling-using-awk/7035.html

How to use AWK command www.computing.net/answers/unix/how-to-use-awk-command/6686.html