Finding a range of numbers of a file in another file using awk

前端 未结 2 815
北恋
北恋 2021-01-24 12:23

I have lots of files like this:

3 
10 
23
.
.
.
720
810
980

And a much bigger file like this:

2 0.004
4 0.003
6 0.034
. 
.
.
99         


        
2条回答
  •  深忆病人
    2021-01-24 13:24

    You can try below:

    for r in *;  do
        awk -v r=$r -F' ' \
        'NR==1{b=$2;v=$4;next}{if(r >= b && r <= $2){m=(v+$4)/2; print m; exit}; b=$2;v=$4}' bigfile.txt
    done
    

    Explanation:

    First pass it saves column 2 & 4 into temp variables. For all other passes it checks if filename r is between the begin range (previous coluimn 2) and end range (current column 2). It then works out the mean and prints the result.

提交回复
热议问题