我有两个大文件。
文件1如下所示:
10 2864001 2864012
10 5942987 5943316文件2如下所示:
10 2864000 28
10 2864001 28
10 2864002 28
10 2864003 27
10 2864004 28
10 2864005 26
10 2864006 26
10 2864007 26
10 2864008 26
10 2864009 26
10 2864010 26
10 2864011 26
10 2864012 26所以我想要创建一个for循环,
因此,上述示例的输出应为文件1第一行文件2第三列的和,即347。我试着使用NR和FNR,但到目前为止我还没有做到。你能帮我生成awk脚本吗?
非常感谢
发布于 2014-01-03 23:46:27
转录,所以可能有打字:
awk '
BEGIN { lastFNR=0; acount=0; FIRST="T"}
FNR < lastFNR {FIRST="F"; aindex=0; next}
FIRST=="T" {
sta[acount] = $2
fna[acount] = $3
acount += 1
lastFNR=FNR
}
FIRST=="F" && $2 >= sta[index] && $2 <= fna[aindex] {
sum[aindex] += $3
lastFNR = FNR
}
FIRST=="F" && $2 > fna[aindex] {
aindex ==1
if (aindex > acount) { FIRST="E" }
}
END {
for(aindex=0; aindex<acount; +=aindex) {
print sta[aindex], "through", fna[index], "totals", sum[aindex]
}
}
' file 1 file2发布于 2014-01-04 12:43:48
你可以试试
awk -f s.awk file1 file2s.awk在哪里
NR==FNR {
a[$1,$2]=$3
next
}
($1,$2) in a {
do {
s+=$3
if ((getline)+0 < 1) break
} while ($2 != a[$1,$2])
print s
}
{ s=0 }产出:
319https://stackoverflow.com/questions/20914260
复制相似问题