awk:打印匹配模式的段落的最后n次出现

awk:打印匹配模式的段落的最后n次出现

我使用该命令根据以下结构将数据段落awk -v RS= '/index/' longfile > dataIwant保存到文件中:dataIwantlongfile

 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

相关内容