匹配模式后提取第 N 行 [英] Extract Nth line after matching pattern

查看:25
本文介绍了匹配模式后提取第 N 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 grepawksed 提取匹配模式后的第 N 行.

I want to extract the Nth line after a matching pattern using grep, awk or sed.

例如我有这样一段文字:

For example I have this piece of text:

      Revision:
      60000<br />

我想提取 60000.

And I want to extract 60000.

我试过 Revision:([a-z0-9]*)s*([0-9]){5} 将 Revision 与修订号相匹配,但是当我通过时到 grep:grep Revision:([a-z0-9]*)s*([0-9]){5} file.html 我什么都没得到.

I tried Revision:([a-z0-9]*)s*([0-9]){5} which matches the Revision together with the revision number but when I pass it to grep: grep Revision:([a-z0-9]*)s*([0-9]){5} file.html I get nothing.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

提取你想要的匹配模式后的第N行:

awk 'c&&!--c;/pattern/{c=N}' file

例如

awk 'c&&!--c;/Revision:/{c=5}' file

将打印文本Revision:"/之后的第 5 行.

would print the 5th line after the text "Revision:"/.

有关详细信息,请参阅使用 sed 或 awk 打印匹配模式后的一行.

See Printing with sed or awk a line following a matching pattern for more information.

这篇关于匹配模式后提取第 N 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆