【发布时间】:2013-03-20 18:56:58
【问题描述】:
我有一个这样的文件:
BOF
-------Title1-------
stuff here
some stuff
-------Title2-------
extra things
extra things
extra things
-------Title3-------
and some stuff
here
...
-------Title4-------
stuff
EOF
现在我想做这样的事情:
grep-by-section KEYWORD DELIMITER my-file
这样
grep-by-section "some" "^---" my-file
输出
-------Title1-------
stuff here
some stuff
-------Title3-------
and some stuff
here
我想找到一个特定的关键字,并且对于每个发现我想输出已知分隔符之间的整个块。我怎样才能做到这一点? sed 让我失望了。
这里的分隔符是“------”,但也可以是其他东西,例如 [0-9]{8} 格式的数字。
一个我无法解决的类似问题是不输出块的内容,只输出块的标题。
使用perl 似乎比sed 更容易解决
【问题讨论】:
-
我认为
awk会是一个很好的解决方案。类似情况见this topic。
标签: regex perl parsing sed grep