Table look up using awk or any other means

Hi all,

I wanted to do a lookup table method using awk or any other commands. I would really appreciate any help on this. my sample input and desired outputs are given below. file1.txt is my lookuptable file, file2.txt, is the main file.

file1.txt

NaN   NaN
33.5    0.00000000000000
33.51   0.041351
33.52   0.082702
33.53   0.124053
33.54   0.165404
33.55   0.206755
33.56   0.248106
33.57   0.289457
33.58   0.330808
33.59   0.372159
33.6    0.41351002500000

file2.txt

33.00 
33.56
33.57
NaN
NaN
33.50
33.60
33.54
NaN
33.71
33.90

desired output

33.00  0.00000000000000 ; 33 < 33.5
33.56  0.041351
33.57  0.289457
NaN  NaN
NaN  NaN
33.50  0.00000000000000
33.60  0.41351002500000
33.54  0.165404
NaN  NaN
33.71  0.41351002500000 
33.90  0.41351002500000 ; 33.9 > 33.6  

If the value in file2.txt is lower than that of the minimum in file1.txt, then it will use the corresponding value of the minimum in file1.txt and will use the maximum value in file2.txt, if it is otherwise.

Try:

awk '
  NR==FNR{
    A[$1]=$2
    if( $1!~/[[:alpha:]]/ ) {
      if( min=="") min=max=$1
      if( $1<min ) min=$1
      if( $1>max ) max=$1
    }
    next
  }
  {
    v=$1
    if( $1!~/[[:alpha:]]/ ) {
      if( $1<min ) v=min
      if( $1>max ) v=max
    }
    print $1, A[v]
  }
' file1 file2
1 Like

Hi scrutinizer,

Thank you very much for the code, it worked smoothly,:slight_smile:

Quick question: what does this condition means?

 if( $1!~/[[:alpha:]]/ )

Thanks much again

Hi, it means "if the first field does not contain a letter" . [:alpha:] is a character class.

1 Like