bash - 使用 AWK 递归查找

标签 bash shell awk pattern-matching

我有一个看起来像这样的文件:

$cat myfile.dat

Number of reps:     nrep=  19230
flop count:         nops=  4725964800.

Clock resolution is  4.7619047619047619E-4 , usecs
time =  7.18247611075639725E-6
calc      0: time=    2.902 Gflop/s=    1.629 error=         0.00000000
calc    201: time=    1.186 Gflop/s=    3.985 error=         0.00000000
Number of reps:     nrep=  13456
flop count:         nops=  4234564800.

Clock resolution is  3.7619047619047619E-4 , usecs
time =  7.18247611075639725E-6
calc      0: time=    1.232 Gflop/s=    2.456  error=         0.00000000
calc    201: time=    3.186 Gflop/s=    1.345  error=         0.00000000

我有兴趣过滤我需要的东西:nreptimeGflop/s 但这最后两个只是开始的行使用 calc 201

到目前为止,除了元素 timeGflop/s 之外,我已经设法过滤了我想要的内容。这就是我所做的:

awk -F'= ?' '/nrep=/||/time=/||/Gflop/{print $2}' myfile.dat

19230
2.902 Gflop/s
1.186 Gflop/s
13456
1.232 Gflop/s
3.186 Gflop/s

这显然是错误的。我需要的,最好是在列中:

19230 1.186 3.985
13456 3.186 1.345

有没有合理的方法来做到这一点?

最佳答案

使用 GNU awk 你只需要做:

$ awk 'NR>1{print $2,$27,$29}' RS='Number of reps:' file
19230 1.186 3.985
13456 3.186 1.345

关于bash - 使用 AWK 递归查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18163954/

相关文章:

linux - 如何避免在 Linux 上创建 tmp 文件?

bash - 交织两个文件bash脚本

regex - 如何将 unicode 字符与 awk 的正则表达式正确匹配?

mysql - sed 从 .csv 中删除单词 + 空格 |优尼克斯 |太阳系

bash - 从 while read 行运行函数

linux - Bash:在 grep 搜索词后显示信息

bash - 如何在没有顶级文件夹的情况下压缩文件但保留子文件夹

shell - 在 shell 脚本中实现无限等待

amazon-web-services - 如何在 AWS CodeBuild 的 buildspec 文件中运行 for 循环?

string - 按 bash 中的行数对文本列进行排序