AWK code for finding distances between atoms in two different files

Hi:)

I have two separate data files (.xyz) type and I want to see distances between the coordinates of atoms of the two files. For example:-
My first files contains
1 1 1 11.50910000 5.17730000 16.49360000
3 1 2 11.21790000 6.36062000 15.60660000
6 1 2 11.43950000 7.66053000 16.07200000
2 1 3 11.87750000 7.81529000 17.04670000
where the last three columns are the coordinates, 1st column is Atom ID, 2nd column is Molecule ID and 3rd column is Atom type. I have another file that has coordinates of different atoms like
14 1 7 9.22151000 9.21624000 11.08350000
21 1 8 8.24299000 10.25310000 11.12120000
7 1 6 9.68004000 8.92467000 9.65365000
22 1 2 11.06970000 3.75903000 16.75830000

I want to make an awk code where AWK will read both files and find distance between atom ID 1 and atom ID 14 and will show the output with the atom IDs and the distance like
1 14 7.1284841 where 7.1284841 is the distance between coordinates of atom ID 1 and atom ID 14. The distance calculating formula is
SQRT ((X1-X2)^2 + (Y1-Y2)^2 + (Z1-Z2)^2)
I want the code to check distance between each single atom in file 1 and all the atoms in file 2. Like, it should give outputs of distances between atom id1 from file 1 and atom IDs 14, 21 7 and 22 from file 2 and then do the same for atom IDs 3, 6 and 2 from file 1. I do not know how to proceed with writing a code for this. I am new to awk.
Please suggest.....:confused:

i'd be happy if someone is generous enough to make this for you. this is quite long,
if you're new to awk i suggest some reading up.
The GNU Awk User's Guide

Thanks ryandegreat25.....i had a look at the manual and I will search it again

I think i can make it easier by using all coordinates in one file......by appending one set to the end of the other.......in that case......i will have 8 lines and I can calculate distance between coordinates given in columns 3,4 and 5 from line 1 with that of the same columns of lines 5,6,7 and 8 and print them accordingly......i am still trying to figure out how to do it

any suggestions.....???????????

Here's a way to do it in awk:

$ 
$ 
$ cat f1
1 1 1 11.50910000 5.17730000 16.49360000
3 1 2 11.21790000 6.36062000 15.60660000
6 1 2 11.43950000 7.66053000 16.07200000
2 1 3 11.87750000 7.81529000 17.04670000
$ 
$ cat f2
14 1 7 9.22151000 9.21624000 11.08350000
21 1 8 8.24299000 10.25310000 11.12120000
7 1 6 9.68004000 8.92467000 9.65365000
22 1 2 11.06970000 3.75903000 16.75830000
$ 
$ cat calcdist.awk
{
  # create associative arrays that store relevant information
  # from both files - atom id and xyz co-ordinates
  if (NR == FNR) {
    x[FNR] = $1":"$4":"$5":"$6
    n = FNR
  } else {
    y[FNR] = $1":"$4":"$5":"$6
  }
}
END {
  # The distance calculating formula is
  # SQRT ((X1-X2)^2 + (Y1-Y2)^2 + (Z1-Z2)^2)
  print "Distance between corresponding atom pairs" 
  print "-----------------------------------------" 
  for (i=1; i<=n; i++) {
    split(x, a, ":")
    split(y, b, ":")
    printf("%2d %2d %.8f\n",a[1],b[1],sqrt((a[2]-b[2])**2 + (a[3]-b[3])**2 + (a[4]-b[4])**2))
  }
  print "Distance between cross-product of atom pairs"
  print "--------------------------------------------" 
  for (i=1; i<=n; i++) {
    split(x, a, ":")
    for (j=1; j<=n; j++) {
      split(y[j], b, ":")
      printf("%2d %2d %.8f\n",a[1],b[1],sqrt((a[2]-b[2])**2 + (a[3]-b[3])**2 + (a[4]-b[4])**2))
    }
  }
}

$ 
$ awk -f calcdist.awk f1 f2
Distance between corresponding atom pairs
-----------------------------------------
 1 14 7.12848415
 3 21 6.64231159
 6  7 6.77413951
 2 22 4.14595714
Distance between cross-product of atom pairs
--------------------------------------------
 1 14 7.12848415
 1 21 8.08046422
 1  7 8.01081509
 1 22 1.50818707
 3 14 5.70951594
 3 21 6.64231159
 3  7 6.66160487
 3 22 2.84897291
 6 14 5.67669318
 6 21 6.43812985
 6  7 6.77413951
 6 22 3.97862564
 2 14 6.67657832
 2 21 7.36641913
 2  7 7.79209489
 2 22 4.14595714
$ 
$ 

HTH,
tyler_durden

thanks a lottt for the code......now i will try to understand what each line of that code means...............
i will edit the code little bit and use it for my long list of coordinates