awk 3 files to one based on multiple columns

Hi all,

I have three files, one is a navigation file, one is a depth file and one is a file containing the measured field of gravity. The formats of the files are;

navigation file:

2006 320 17 39 0 0 *nav 21.31542 -157.887
2006 320 17 39 10 0 *nav 21.31542 -157.887
2006 320 17 39 20 0 *nav 21.31542 -157.887

depth file:

2006 321 19 17 16 681 dpth 4744.62 0
2006 321 19 17 31 419 dpth 4741.73 0
2006 321 19 17 46 973 dpth 4744.53 0
2006 321 19 18 2 26 dpth 4745.93 0

gravity file:

2006 320 17 39 30 0 cgrv 6992.7 205.6 0 978923
2006 320 17 40 0 0 cgrv 6992.7 205.6 -0.1 978923.1
2006 320 17 40 30 0 cgrv 6992.8 205.7 0 978923.1
2006 320 17 41 0 0 cgrv 6992.8 205.7 0 978923.2

The first five columns of each file are time stamps (year, day, hour, min, seconds). I want to find the latitude (column 8, file 1), longitude (column 9, file 1) and depth (column 9, file 2) for the matching times of file 3 (gravity file). So I need to find when the first 5 columns of file 1 are equal to the first five columns of file 3 and append column 8 and 9 of file 1 to a new version of file 3. I then need to find when the first 5 columns of file 2 are equal to the first five columns of file 3 and append column 9 of file 2 to the new file.

Did that make any sense?

Thanks!

#!/bin/sh
FILE1="gravity.txt"
FILE2="navigation.txt"
FILE3="depth.txt"

while read line1
do
  key=`echo $line1 | cut -d ' ' -f 1-5`
  line2=`grep "$key" $FILE2 | head -1 | cut -d ' ' -f 8-9`
  line3=`grep "$key" $FILE3 | head -1 | cut -d ' ' -f 9`
  echo "$line1 $line2 $line3"
done < $FILE1

(Untested)

The way I'd do it would be to read file 3 line by line using sed or awk to get get the columns, untested as I'm not near a Unix machine.

file3=`cat $filename | awk '{FS="(Whatever it is)"};{OFS="Whatever"};{print $1 - $5}`

for i in $file3
do
awk '{OFS=""};{OFS=""};{/$i/};{print $i $9}' file1 > opfile.
do for file 2
done

The syntax is worng but the general idea works.

Then sort the output file by date.