bash, find nearest next value, forward and backward

前端 未结 4 1026
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 08:51

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.         


        
4条回答
  •  攒了一身酷
    2021-01-16 09:21

    I don't know if this is what you're looking for, but this is what I came up with, not knowing awk:

    #!/bin/sh
    
    IFSBAK=$IFS
    IFS=$'\n'
    
    best=
    
    for line in `cat $1`; do
        IFS=$' \t'
        arr=($line)
    
    
        num=${arr[5]}
        [[ -z $best ]] && best=$num
    
        if [ $(bc <<< "$num < 62.997") -eq 1 ]; then 
            if [  $(bc <<< "$best < $num") -eq 1 ]; then
                best=$num
            fi
        fi
    
        IFS=$'\n'
    done
    
    IFS=$IFSBAK
    echo $best
    

    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.

提交回复
热议问题