我正在寻找在使用“猫”时获得行号的方法。
我在Ubuntu16.04上像这样尝试了这个命令

cat flux.log |grep "statistic"

这就是我们发现信息的结果。
 Using fit statistic: chi
 Using test statistic: chi
Fit statistic : Chi-Squared =         175.92 using 16 PHA bins.
Test statistic : Chi-Squared =         175.92 using 16 PHA bins.
Fit statistic : Chi-Squared =         175.92 using 16 PHA bins.
Test statistic : Chi-Squared =         175.92 using 16 PHA bins.
 Fit statistic in use: Chi-Squared
 Using fit statistic: chi
 Using test statistic: chi
Fit statistic : Chi-Squared =         175.92 using 16 PHA bins.
Test statistic : Chi-Squared =         175.92 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Test statistic : Chi-Squared =          66.15 using 16 PHA bins.
 Fit statistic in use: Chi-Squared
 Using fit statistic: chi
 Using test statistic: chi
Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Test statistic : Chi-Squared =          66.15 using 16 PHA bins.

在这个结果中,我想要最后一行来捕捉信息。
Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Test statistic : Chi-Squared =          66.15 using 16 PHA bins.

问题是有很多行,这些数字是随机的。所以我只需要找到诸如“拟合”、“检验”、“统计”或“卡方”之类的信息。
如果这些结果有行号并且可以区分,我可以找到我想要的行。有人帮我吗?
PS,我试过这个命令
<cat -n flux.log |grep "statistic">

但是每一个文件的所有行都是不同的。

最佳答案

组合cat/grep通常可以替换为单个awk
在您的情况下,您不知道最后两次出现的时间,因为您的文件可以跨越随机行数。尝试以下方法:

$ awk '/statistic/{first= last;last="Line" NR " : " $0}END{print first RS last}'  casefile_48436615
Line27 : Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Line28 : Test statistic : Chi-Squared =          66.15 using 16 PHA bins.

信息:awk内置的NR给出了记录号,这确实是您要查找的行号。还有RS是awk内置的默认记录分隔符,它是一个换行符。使用rs可以帮助我们在脚本中解决硬编码换行的问题。

10-04 10:56
查看更多