AWK Match to nearest number

Hello Guys,

I'm very new on here and require some help matching up and printing some columns using awk.

I have two text files. The first file has Longitude data in column 1 (lon.txt) and the second one (node.txt) has again another Longitude data in column 1 (not exact as the first one) + in the second column it has a node number.

Now I need to match the closest Longitude number from the first file to the second file and print the node number.

Please note that the files sizes are also not the same. The first files has 29,572 lines, while the second file has 1755,555 lines.

How could this be best approached?

Many thanks
Ian

Hi.

Welcome to the forum. For problem sets that are not homework, school-related:

Best wishes ... cheers, drl

1 Like

here is a quick one...haven't tested it

ABS_DIFF=360

cat lon.txt | while read LONG_1
do
    cat node.txt | while read LINE
    do
        LONG_2=`echo $LINE | cut -f1 -d " "`
        ABS_DIFF_NEW=`expr $LONG_1 - $LONG_2 | awk '{ print ($1 >= 0) ? $1 : 0 - $1}'`
        if (($ABS_DIFF_NEW < $ABS_DIFF)); then 
                ABS_DIFF=$ABS_DIFF_NEW
                LONG=`echo $LINE | cut -f1 -d " "`
                NODE_NO=`echo $LINE | cut -f2 -d " "`
        fi
    done
    
    echo "from lon.txt - $LONG_1 , from node.txt $LONG and $NODE_NO"
done

Thanks aster007,

I will put it to the test tomorrow, and will let you know how I got on.

Many thanks again :slight_smile:

Cheers,
Ian

---------- Post updated 04-13-11 at 03:33 AM ---------- Previous update was 04-12-11 at 01:09 PM ----------

Hi aster007

I tottaly missed this up!! :frowning:

I will try and explain again.......

I have two files lat_lon.txt and node.txt

lat_lon.txt has 1755642 lines in and in collumn 1 has lat numbers and in collumn 2 has lon numbers
eg..
Latitude Longitude 57.14824 -2.09665 57.14871 -2.09781 57.14824 -2.09665 57.14808 -2.09467 57.14843 -2.09797 57.14962 -2.09533 57.14973 -2.09474 57.14867 -2.09599
node.txt has 29648 lines in and in collumn 1 it has a node number and in collumn 2 has lat number and in collumn 3 it has a lon number
eg
1 48.39 -13 2 48.39 -12.89 3 48.39 -12.78 4 48.39 -12.67 5 48.39 -12.56 6 48.39 -12.45 7 48.39 -12.34 8 48.39 -12.23 9 48.39 -12.12 10 48.39 -12.01
Now what I need to do is to cloest match the lon and lat numbers from lon_lat.txt to the lon and lat numbers from node.txt and print the node number is a new file.

I hope this is making sense!

thanks,

I'll repeat the request of our Forum Advisor drl:

OK thanks,

I have attached sample files of the node.txt and the lat_lon.txt

I need to match the lat and lon from lat_lon.txt to the lat lon from node.txt and print the node number in a new text file.

Many thanks
Ian

It's still unclear.. these are the files:

node.txt

1	48.39	-13
2	48.39	-12.89
3	48.39	-12.78
4	48.39	-12.67
5	48.39	-12.56
6	48.39	-12.45
7	48.39	-12.34
8	48.39	-12.23
9	48.39	-12.12
10	48.39	-12.01
11	48.39	-11.9
12	48.39	-11.79
13	48.39	-11.68

lat_lon.txt

57.148235	-2.096648
57.14871	-2.097806
57.148235	-2.096648
57.148084	-2.094665
57.148431	-2.097971
57.149619	-2.09533
57.149727	-2.094735
57.148667	-2.095988
57.148084	-2.094665

What should be the desired output?

The output would be to match the closet lat and lon numbers from from lat_lon.txt to the node.txt lat and lon numbers and return the node number.

Cheers,

---------- Post updated at 05:17 AM ---------- Previous update was at 05:12 AM ----------

So on line one of the lat_lon.txt
57.148235 -2.096648
The closest match on the node.txt is

17323 57.08 -1.89