查找范围中使用其他文件的文件的数量的AWK [英] Finding a range of numbers of a file in another file using awk

查看:108
本文介绍了查找范围中使用其他文件的文件的数量的AWK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的文件是这样的:

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
. 
.
.
996 0.01
998 0.02
1000 0.23

我想要做的就是找​​到在第二个文件的范围内我的第一个文件下降,然后估算值的平均值在该范围内的第2列。

What I want to do is find in which range of the second file my first file falls and then estimate the mean of the values in the 2nd column of that range.

先谢谢了。

请注意

在文件中的数字不一定遵循一个简单的模式就像2,4,6 ...

The numbers in the files do not necessarily follow an easy pattern like 2,4,6...

推荐答案

由于您较小的文件进行排序,你可以拉出第一排和最后一排,以获得最小和最大。然后你只需要经过大文件与awk脚本来计算的平均值。

Since your smaller files are sorted you can pull out the first row and the last row to get the min and max. Then you just need go through the bigfile with an awk script to compute the mean.

所以,每个小文件您可以运行脚本

So for each smallfile small you would run the script

awk -v start=$(head -n 1 small) -v end=$(tail -n 1 small) -f script bigfile

其中,剧本可以像

BEGIN {
    sum = 0;
    count = 0;
    range_start = -1;
    range_end = -1;
}
{
    irow = int($1)
    ival = $2 + 0.0
    if (irow >= start && end >= irow) {
            if (range_start == -1) {
                range_start = NR;
            }
            sum = sum + ival;
            count++;
        }
    else if (irow > end) {
            if (range_end == -1) {
                range_end = NR - 1;
            }
        }
}
END {
    print "start =", range_start, "end =", range_end, "mean =", sum / count
}

这篇关于查找范围中使用其他文件的文件的数量的AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆