I have a data.txt file
1 2 3 4 5 6 7
cat data.txt
13 245 1323 10.1111 10.2222 60.1111 60.22222
13 133 2325 11.2222 11.333 61.2222 61.3333
13 245 1323 12.3333 12.4444 62.3333 62.44444444
13 245 1323 13.4444 13.5555 63.4444 63.5555
Find next nearest: My target value is 11.6667
and it should find the nearest next value in column 4
as 12.3333
Find previous nearest: My target value is 62.9997
and it should find the nearest previous value in column 6
as 62.3333
I am able to find the next nearest (case 1) by
awk -v c=4 -v t=11.6667 '{a[NR]=$c}END{
asort(a);d=a[NR]-t;d=d<0?-d:d;v = a[NR]
for(i=NR-1;i>=1;i--){
m=a[i]-t;m=m<0?-m:m
if(m<d){
d=m;v=a[i]
}
}
print v
}' f
12.3333
Any bash solution? for finding the previous nearest (case 2)?
Try this:
.
I don't know if this is what you're looking for, but this is what I came up with, not knowing
awk
:If you want, you can add the column and the input value
62.997
as paramters, I didn't to demonstrate that it would look for specifically what you want.Edited to remove assumption that file is sorted.
For the first part:
And second part:
Both in one go:
You solution looks unnecessarily complicated (storing a whole array and sorting it) and I think you would see the
bash
solution if you re-thought yourawk
.In
awk
you can detect the first line withso on the first line, set a variable
BestYet
to the value in the column you are searching.On subsequent lines, simply test if the value in the column you are checking is
if it is, update
BestYet
. At the end, printBestYet
.In
bash
, apply the same logic, but read each line into a bash array and use${a[n]}
to get the n'th element.