从文本文件中选择一组中的几行

时间:2013-12-20 14:42:14

标签: sed awk grep

我有一个包含3500行的长文本文件。该文件的内容如下:

 Iteration 26
 WSSR        : 2.16978           delta(WSSR)/WSSR   : -5.96505e-05
 delta(WSSR) : -0.000129428      limit for stopping : 1e-05
 lambda   : 1.92367e-12

resultant parameter values

A               = 0.152297
B               = 22009.9
/

 Iteration 27
 WSSR        : 2.16978           delta(WSSR)/WSSR   : -6.95237e-07
 delta(WSSR) : -1.50851e-06      limit for stopping : 1e-05
 lambda   : 1.92367e-13

resultant parameter values

A               = 0.152268
B               = 22018.5

After 27 iterations the fit converged.
final sum of squares of residuals : 2.16978
rel. change during last iteration : -6.95237e-07

degrees of freedom    (FIT_NDF)                        : 15996
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 0.0116467
variance of residuals (reduced chisquare) = WSSR/ndf   : 0.000135645

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

A               = 0.152268         +/- 0.0003536    (0.2322%)
B               = 22018.5          +/- 73.18        (0.3324%)


correlation matrix of the fit parameters:

               A      B      
A               1.000 
B              -0.712  1.000 
Max. number of data points scaled up to: 3072
Max. number of data points scaled up to: 4608
Max. number of data points scaled up to: 6912
Max. number of data points scaled up to: 10368
Max. number of data points scaled up to: 15552
Max. number of data points scaled up to: 23328


 Iteration 0
 WSSR        : 92.7915           delta(WSSR)/WSSR   : 0
 delta(WSSR) : 0                 limit for stopping : 1e-05
 lambda   : 0.000192367

initial set of free parameter values

C               = 1
D               = 1
***/

 Iteration 1
 WSSR        : 92.7561           delta(WSSR)/WSSR   : -0.000382419
 delta(WSSR) : -0.0354717        limit for stopping : 1e-05
 lambda   : 0.0192367

resultant parameter values

C               = 1.09786
D               = 1.4932
/

 Iteration 2
 WSSR        : 91.9873           delta(WSSR)/WSSR   : -0.00835736
 delta(WSSR) : -0.768771         limit for stopping : 1e-05
 lambda   : 0.00192367

我想从整个文本文件中的最终参数集渐近标准错误开始选择6条特定行。这组行不会在文本文件中以周期性方式出现。有时它发生在10行或30行之后。

我能想到grep命令。但我有经验使用它只选择具有特定单词的特定行。像这样的东西

grep "Final set of parameters"

但是,是否需要添加任何选项以实现我的需求?

我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

 grep -A6 'Final set of parameters' file

给出:

kent$  grep -A6 'Final set of parameters' f
Final set of parameters            Asymptotic Standard Error
=======================            ==========================

A               = 0.152268         +/- 0.0003536    (0.2322%)
B               = 22018.5          +/- 73.18        (0.3324%)

答案 1 :(得分:2)

您也可以使用awk

awk '/Final set of parameters/ {f=6} f && f-- '
Final set of parameters            Asymptotic Standard Error
=======================            ==========================

A               = 0.152268         +/- 0.0003536    (0.2322%)
B               = 22018.5          +/- 73.18        (0.3324%)