通过多行grep信息

时间:2018-04-01 12:56:37

标签: regex grep

我有这个信息:

not what i want
ul
text1
text1
/ul
not what i want
not what i want
ul
texttext2
texttext2
texttext2
/ul
not what i want

如何在'ul'和'/ ul'之间获取数据?

我试过了grep -oP 'ul.*?/ul'

我想要的输出是:

ul
text1
text1
/ul
ul
texttext2
texttext2
texttext2
/ul

2 个答案:

答案 0 :(得分:1)

使用此:

awk '/ul/,/\/ul/' file

sed -n '/ul/,/\/ul/p' file

perl -lne 'print if /ul/ .. /\/ul/' file

答案 1 :(得分:1)

grep和bash流程替换的帮助下使用tr和PCRE:

$ grep -oP ul.*?ul <(cat file | tr '\n' _) | tr _ '\n'
ul
text1
text1
/ul
ul
texttext2
texttext2
texttext2
/ul
  • <(cat file | tr '\n' _)用下划线替换换行符
  • grep -oP ul.*?ul获取ul—/ul范围
  • tr _ '\n'将下划线替换为换行符