如何在匹配字符串后提取n行并使用awk / bash同时选出第二列?

时间:2017-05-11 17:57:37

标签: bash awk

我有一个文件说:

Prediction of step:
Time = 45 s

.

.

.

.

6.45 3.99 7.40 9.22 #this is the 10th line after 'Prediction of step'

.

.

.

我希望提取“预测步骤:”和之后的行,以及第10行的第二列,即3.99: 所以我的输出是:

Prediction of step:

Time = 45 s

3.99

到目前为止,我已设法撤出所有三条线:

awk '/Prediction of step:/{nr[NR]; nr[NR+1]; nr[NR+10]}; NR in nr' file > new_data

但是有可能拉出前两行而只能拉出第10行的第二列吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

$ awk '/Prediction of step:/{c=1} c{ if (c++<3){print} else if (c==11){print $2; c=0} }' file
Prediction of step:
Time = 45 s
3.99