![awk:打印匹配模式的段落的最后n次出现](https://linux22.com/image/214700/awk%EF%BC%9A%E6%89%93%E5%8D%B0%E5%8C%B9%E9%85%8D%E6%A8%A1%E5%BC%8F%E7%9A%84%E6%AE%B5%E8%90%BD%E7%9A%84%E6%9C%80%E5%90%8En%E6%AC%A1%E5%87%BA%E7%8E%B0.png)
我使用该命令根据以下结构将数据段落awk -v RS= '/index/' longfile > dataIwant
保存到文件中:dataIwant
longfile
index value count
1 -32.4613 1.00000
2 -20.5946 1.00000
3 -13.3573 1.00000
4 -13.2064 1.00000
5 -13.2064 0.00000
other stuff
index value count
1 -31.4613 1.00000
2 -19.5946 1.00000
3 -12.3573 1.00000
4 -12.2064 1.00000
5 -12.2064 0.00000
still a lot of other stuff
index value count
1 -33.4613 1.00000
2 -21.5946 1.00000
3 -15.3573 1.00000
4 -15.2064 0.00000
5 -15.2064 0.00000
again a lot of other stuff (and so on many times)
如何仅将表“索引、值、计数”的最后 n (ex 2) 次出现而不是全部保存到文件?
预期输出:
index value count
1 -31.4613 1.00000
2 -19.5946 1.00000
3 -12.3573 1.00000
4 -12.2064 1.00000
5 -12.2064 0.00000
index value count
1 -33.4613 1.00000
2 -21.5946 1.00000
3 -15.3573 1.00000
4 -15.2064 0.00000
5 -15.2064 0.00000
答案1
只需保留最后 n 条记录的滚动缓冲区并在 END 中打印它们:
$ cat tst.awk
BEGIN { RS=""; ORS="\n\n" }
/index/ { recs[(++c)%n] = $0 }
END {
for ( i=1; i<=n; i++ ) {
print recs[(++c)%n]
}
}
$ awk -v n=2 -f tst.awk file
index value count
1 -31.4613 1.00000
2 -19.5946 1.00000
3 -12.3573 1.00000
4 -12.2064 1.00000
5 -12.2064 0.00000
index value count
1 -33.4613 1.00000
2 -21.5946 1.00000
3 -15.3573 1.00000
4 -15.2064 0.00000
5 -15.2064 0.00000
答案2
将匹配的段落保存在数组中,并在END
块中打印最后一个n
元素,例如 for n=3
:
awk -v n=3 -v RS= '/index/{z[i++]=$0};END{for (j=i-n; j<=i; j++) print z[j]}' in > out