AWK Match to nearest number | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

AWK Match to nearest number

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-12-2011
ian_gooch ian_gooch is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 16 January 2012, 3:52 AM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 04-12-2011
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 25 November 2014, 12:28 PM EST
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,703
Thanks: 42
Thanked 201 Times in 183 Posts
Hi.

Welcome to the forum. For problem sets that are not homework, school-related:
Quote:
To obtain the best answers quickly when you want help in
processing datasets -- extracting, transforming, filtering,
then:

1. Post representative samples of your data (i.e. data that
should "succeed" and data that should "fail")

2. Post what you expect the results to be, instead of trying
only to describe them.

3. Post what you have attempted to do so far.

4. Place the data within CODE tags, so that it more easily
readable.

Special cases, exceptions, etc., are very important to include
in the samples.
Best wishes ... cheers, drl
The Following User Says Thank You to drl For This Useful Post:
Franklin52 (04-13-2011)
Sponsored Links
    #3  
Old 04-12-2011
aster007 aster007 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 8 May 2014, 4:13 PM EDT
Location: Dallas, TX
Posts: 46
Thanks: 14
Thanked 9 Times in 7 Posts
here is a quick one...haven't tested it


Code:
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

    #4  
Old 04-13-2011
ian_gooch ian_gooch is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 16 January 2012, 3:52 AM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks aster007,

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

Many thanks again :-)

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!! :-(

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,
Sponsored Links
    #5  
Old 04-13-2011
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 25 November 2014, 4:53 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
I'll repeat the request of our Forum Advisor drl:

Quote:
To obtain the best answers quickly when you want help in
processing datasets -- extracting, transforming, filtering,
then:

1. Post representative samples of your data (i.e. data that
should "succeed" and data that should "fail")

2. Post what you expect the results to be, instead of trying
only to describe them.

3. Post what you have attempted to do so far.

4. Place the data within CODE tags, so that it more easily
readable.

Special cases, exceptions, etc., are very important to include
in the samples.
Sponsored Links
    #6  
Old 04-13-2011
ian_gooch ian_gooch is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 16 January 2012, 3:52 AM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
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
Attached Files
File Type: txt node.txt (208 Bytes, 7 views)
File Type: txt lat_lon.txt (187 Bytes, 5 views)
Sponsored Links
    #7  
Old 04-13-2011
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 25 November 2014, 4:53 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
It's still unclear.. these are the files:

node.txt

Code:
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

Code:
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?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Match and count the number of times cdfd123 Shell Programming and Scripting 1 05-24-2010 09:37 AM
sed match closest/nearest pattern sudheer1984 UNIX for Advanced & Expert Users 1 07-02-2009 04:59 AM
Rounding off decimals to the nearest number in PERL rdlover Shell Programming and Scripting 9 03-30-2009 01:56 AM
match nearest bogu0001 UNIX for Dummies Questions & Answers 1 02-13-2009 04:42 AM
Match String and get line number and filename thenz Shell Programming and Scripting 5 12-22-2008 05:17 AM



All times are GMT -4. The time now is 04:37 PM.