Link to home
Start Free TrialLog in
Avatar of Anthony Mellor
Anthony MellorFlag for United Kingdom of Great Britain and Northern Ireland

asked on

awk and Pythagoras?

this is a follow on from here:
https://www.experts-exchange.com/questions/29004558/CSV-How-to-add-columns-based-on-existing-column-s.html

Pythagoras says (amongst many other things) that if you have two sides of a triangle you can derive the third with this equation

square root of (((a+c)^2)+((b+d)^2))

where a b c & d are ordnance survey coordinates called Eastings and Northings that might look like these:

a = 530390
b= 172970

c =530392
d =172972

ans is: 2.82842712474619 metres if I have that right.

Question is can awk do this sort of thing?

Without the square root and using ((a+c)*(a+c))+((b+d)*(b+d)) is acceptable for further processing as it gives us the nearest location when in a list of others - the volume of data is 3 to 6 million records depending, compared to another list with about 3,000 records, which is where Excel falls over (literally), though I now have array formulae that work (but NOT on 6m records), using awk might be very interesting.
Avatar of Bill Prew
Bill Prew

If I use your data and formula I get an answer of 1115766.247423, can you double check your results.

Here is how that can be done in AWK, just a simple example you can test there...

BEGIN {
   a = 530390
   b = 172970
   c = 530392
   d = 172972
   x = sqrt(((a+c)^2)+((b+d)^2))
   printf("%f\n", x)
}

Open in new window

~bp
Avatar of Anthony Mellor

ASKER

duh, my stupid mistake, it's minus not plus

square root of (((a-c)^2)+((b-d)^2))

script works (meaning the right answer) when + changed to minuses

I do like the way you effectively declared a load of variables without having to do anything
add the solution below and I'll tick that , up for another one?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm thinking if I follow this awk road, it may be better to append, or rather "prepend" the new columns at the beginning of the rows than at the end, so keeping the columns I am actually going to work on in the low numbers of the column count, not least because if I later delete redundant columns I won't have to rewrite the scripts because columns have changed number after intervening ones have been deleted.