linux - How to find grid points nearest to given location using shell script? -


i have 2 different files. part of files are:

file 1:

ifile1.txt 21   10.3   70.32  09   32   11.3   71.43  10 33   15.4   75.00  00 54   17.3   68.03  12 95   19.2   65.02  21 99   20.1   80.10  11 , on......  1st column id,        2nd column refers x-axis,        3rd column refers y-axis,       4th column refers value 

file2:

ifile2.txt 10.10  70.12  10   10.11  73.33  08 11.05  72.40  00 11.30  69.13  15 12.00  64.02  27 12.05  79.20  25 13.10  80.32  10   13.11  75.43  06 14.05  74.00  02 14.20  69.03  15 16.40  65.02  13 16.55  68.10  14 , on......  1st column refers x-axis,        2nd column refers y-axis,       3rd column refers value 

i llike make file output (for above example):

ofile.txt 21   10.10  70.12  10   32   11.05  71.40  10 33   14.05  74.00  02 54   16.55  68.10  14 95   16.55  68.10  14 99   16.55  68.10  14  1st column id (taken ifile1.txt),        2nd , 3rd column refers x-axis , y-axis (taken ifile2.txt nearest of ifile1.txt),       4th column refers corresponding value (taken ifile2.txt) 

i can't able write shell script this. algorithm is:

for each $1 in ifile1.txt,    each $2 & $3 in ifile1.txt,      find $2 & $3 in ifile2.txt such        sqrt{[$2(ifile1.txt)-$2(ifile2.txt)]^2+[$3(ifile1.txt)-$3(ifile2.txt)]^2} minimum $2 & $3 in ifile2.txt      find correcsponding $4 ifile2.txt     write $1, $2, $3, $4 ofile.txt 

i highly appreciate kind of suggestions or help.

here simple solution in awk. worked fine @ linux. if answer useful please click grey tick sign on left side of answer.

#!/bin/bash (($#!=2))&& { echo "usage $0 1st_file 2nd_file"; exit 1; } awk '  begin {p=fx=0; fn=""; maxd=1.1e11;}  $0~"[^0-9. \t]" || nf!=4 && nf!=3 {next;}          # skip no data lines  fn!=filename {fx++; fn=filename;}                  # fx: file  fx==1 { if(nf!=3){printf("change series of 2 input files\n"); exit 1;}          x2[p]=$1; y2[p]=$2; d2[p++]=$3;next;}      # save columns of first file  fx==2 { mv=maxd; mp=0;                             # search minimal distance            for(i=0; i<p; i++){               dx=x2[i]-$2; dy=y2[i]-$3; dd=dx*dx+dy*dy;               if(dd<mv){mv=dd; mp=i;}               # min value & min place            }            printf("%3d %6.2f %6.2f %3d\n", $1, x2[mp], y2[mp], d2[mp]);          } ' $2 $1                                             # first 2nd_file! 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -