Grab a smaller and larger value

Hi All,

I am trying to grab a term which is just smaller and larger than the assigned value using the below code. But there seems to be some problem. The value i assign is 25 so i would expect it to output a smaller value to be 20 instead of 10 and 20 and larger value to be 30 instead of 30 and 40. Can anybody help ?

Input:

10
20
30
40

Output:
My Value = 25
Smaller Value = 20
Bigger Value = 30

awk '$1<= 25 END{print $1}' input
awk '$1>= 25 BEGIN{print $1}' input

read up on 'man awk'

echo '20' | awk '{printf("%s Value = %d\n", ($1 < 25) ? "Smaller" : "Bigger", $1)}'

nawk -f ray.awk inputFile

ray.awk:

BEGIN {
  myValue=25
  bigger=9999999999
}
{
  if ( $1 < myValue && $1 > smaller ) smaller = $1
  if ( $1 > myValue && $1 < bigger )  bigger = $1
}
END {
   printf("My Value = %d\nSmaller Value = %d\nBigger Valuer = %d\n", myValue, smaller, bigger)
}

Hi,

To grab
the smaller value : awk '{if($1<=25) {print $1;}}' input | sort | tail -1
the larger value : awk '{if($1>=25) {print $1;}}' input | sort -r | tail -1

Regards,
Chella

Hi vgersh99 & chella,

Thanks alot for your help!!!!!