添加在AWK循环 [英] Adding a loop in awk
问题描述
我这是在previous <一个解决的问题href=\"http://stackoverflow.com/questions/22145261/finding-a-range-of-numbers-of-a-file-in-another-file-using-awk\">post:
I had a problem that was resolved in a previous post:
但是,因为我有太多的文件,这是不切合实际做的每一个文件一个awk,然后使用第二个脚本来得到我想要的输出。
But because I had too many files it was not practical to do an awk on every file and then use a second script to get the output I wanted.
下面是我的文件的一些例子:
Here are some examples of my files:
3
10
23
.
.
.
720
810
980
和脚本用于看到从第一个文件的数字在这个其他的文件下跌:
And the script was used to see where the numbers from the first file fell in this other file:
2 0.004
4 0.003
6 0.034
.
.
.
996 0.01
998 0.02
1000 0.23
这范围位于后,将第二列中的第二文件的平均值估计
After that range was located, the mean values of the second column in the second file was estimated.
下面是脚本:
awk -v start=$(head -n 1 file1) -v end=$(tail -n 1 file1) -f script file2
和
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
}
我如何才能让一个循环,这样每个文件的平均估计。我期望的输出会是这样的:
How could I make a loop so that the mean for every file was estimated. My desired output would be something like this:
Name_of_file
start = number , end = number , mean = number
先谢谢了。
推荐答案
..把它包在一个循环?
.. wrap it in a loop?
for f in <files>; do
echo "$f";
awk -v start=$(head -n 1 "$f") -v end=$(tail -n 1 "$f") -f script file2;
done
个人而言,我会建议他们在同一行合并(使你的结果是数据块的,而不是在不同的线路从他们的成果文件名 - 在这种情况下替换回声$ F
与回声-n$ F
(不添加新行)。
编辑:由于我想你是新来的语法,&LT;文件&GT;
可以是文件列表(文件1文件2文件3
),由一个水珠(文件*
,文件/数据_ *。TXT 生成文件列表code>,等等),或者通过一个命令生成的文件列表(
$(查找文件/ -name'数据'型的F)
等)。
Since I suppose you're new to the syntax, <files>
can either be a list of files (file1 file2 file 3
), a list of files as generated by a glob (file*
, files/data_*.txt
, whatever), or a list of files generated by a command ( $(find files/ -name 'data' -type f)
, etc).
这篇关于添加在AWK循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!