Grepping出的文本块,正则表达式 [英] Grepping out a block of text, regex

查看:133
本文介绍了Grepping出的文本块,正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于大的日志文件,究竟是到grep一个文本块的最佳方式?

Given a large log file, what is the best way to grep a block of text?

text to be ignored
more text to be ignored
---                                 <---- start capture here
lots of 
text with separators like "---"
---
spanning 
multiple lines
---                                 <---- end capture here
text to be ignored
more text to be ignored

什么是已知的?


  • 最大数行的字符(55,但可能会少)

  • 行的块号

  • 分离器(可能重演)

什么正规的前pression会匹配这个块?所需的输出:文本块列表

What regular expression would match this block? Desired output: list of blocks of text.

请假设Linux命令行环境

Please assume Linux command line environment

推荐答案

几年前,我用这个补丁分割成大块:

Several years ago I used this to split patches into hunks:

sed -e '$ {x;q}' -e '/@@/ !{H;d}' -e '/@@/ x' # note - i know sed better now

替换 / @@ / / --- /

要之前首先---后最后---添加<$删除一切C $ C> -e'1,/ --- / D 键,删除整个 -e'$ {X,q}

To remove everything before first '---' and after last '---' add -e '1,/---/d' and remove the whole -e '$ {x;q}'.

结果会是这样:

sed -e '1,/---/d' -e '/---/ !{H;d}' -e x

只是测试它,并将它与给定的示例工作。

Just tested it and it works with the given example.

这篇关于Grepping出的文本块,正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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