# Calculating distances using awk

March 2, 2010 at 01:18:46
Specs: Linux i686
 Dear experts,I have a total of 1024 lines of data in x y z Cartesian coordinate format in three columns. Say first line is coordinate for A point. second line is for B point, third line is for C point and fourth line is for D point. What I want to calculate is the distances in between A point to B point, A point to C point and finally A point to D point. After having three distances AB, AC, and AD than I want to compare those distances and print the maximum distance out in a file called distances.dat.Since I have 1024 lines of data, I want to do this calculation for every 4 lines subsequently. That means I will have 64 final data in the distances.dat file. CAN anyone guide me how to write this in AWK language?

See More: Calculating distances using awk

#1
March 2, 2010 at 19:26:24
 do you mean like this:A1: xa1,ya1,za1B1: xb1,yb1,zb1C1: xc1,yc1,zc1D1: xd1,yd1,zc1A2: xa2,ya2,za2...and you want to do all three coords?tx1=abs(xa1-xb1)tx2=abs(xa1-xc1)tx3=abs(xa1-xd1)ty1=abs(ya1-yb1)dah dah dah...then report the greatest of the three tees for each coord.kind of a 4-dimension matrix operation... (if you count the subsequent blocks of 4 lines as a fourth dimension.)if that's the case, i'm not sure AWK has that power, but a combination of utilities might achieve it. kind of a bitch to try and do using cat, AWK, READ, and the built-in math ops.I would seriously try to use an actual language versus trying to do it in script, although it could probably be done in script.I don't think, with my sucky shell scripting ability, i will take this one on. It would take me hours, if not longer...I could write one in BASIC, or a good C programmer could write one in C in about 5 minutes. not to mention PERL or Python (neither of which I have learned).maybe post in the unix forum and you'll get someone who can whip out a script-only solution. If you want to approach from a prog-language angle, let us know.

Report •

#2
March 2, 2010 at 22:52:49
 Hi nbrane,Thanks for your reply.If it sounds very difficult to do in awk script than is that possible to write in fortran code?

Report •

#3
March 3, 2010 at 22:19:46
 from what i remember of fortran, it should be fairly easy to convert from BASIC. i wrote in BASIC since my last (and only) FORTRAN course was a 1-hour quickie about 25 years ago. here is a crappy version to start with, as i am not sure how you want output handled regarding 3 different coords.:OPEN "dis" FOR INPUT AS #1OPEN "dists.out" FOR OUTPUT AS #2ONE:A(5, 1) = -1FOR J = 1 TO 4FOR I = 1 TO 3IF EOF(1) THEN GOTO XITINPUT #1, A(J, I)NEXT INEXT JGOSUB DISPFOR I = 1 TO 3FOR J = 2 TO 4TEST = ABS(A(1, I) - A(J, I))IF TEST > A(5, I) THEN A(5, I) = TESTNEXT JNEXT IIF NOT EOF(1) THEN GOTO ONEXIT:CLOSEDISP:FOR I = 1 TO 3IF A(5, 1) > -1 THEN PRINT #2, A(5, I); ",";A(5, I) = 0NEXT IPRINT #2, ""----------- end ----------this just craps out the highest value of each of the three X, Y, Z differences. My guess is you will need more than that, so i won't spend a lot of effort till i see.

Report •

Related Solutions

#4
March 8, 2010 at 18:54:18
 Hi nbrane,I would like to say thanks to you. You have given the clue. I have tried using AWK, BASH and little Fortran CODE and it works.Thanks again.Regards

Report •