Grep获取字符串并读取内容,直到下一个匹配字符串

时间:2019-06-19 17:41:51

标签: search awk sed grep

我正在尝试读取文件并使用grep搜索字符串。找到字符串后,我想读取字符串之后的所有内容,直到匹配另一个字符串。因此,在我的示例中,我正在搜索...SUMMARY...,并且想要读取所有内容,直到出现...,这是一个示例:

**...SUMMARY...**
   Severe thunderstorms are most likely across north-central/northeast
   Texas and the Ark-La-Tex region during the late afternoon and
   evening. Destructive hail and wind, along with a few tornadoes are
   possible. Severe thunderstorms are also expected across the
   Mid-South and Ohio Valley.

   **...**North-central/northeast TX and southeast OK/ArkLaTex...
   In the wake of a decaying MCS across the Lower Mississippi River
   Valley, a northwestward-extending outflow boundary will continue to
   modify/drift northward with rapid/strong destabilization this
   afternoon particularly along and south of it. A quick
   reestablishment of lower/some middle 70s F surface dewpoints will
   occur into prior-MCS-impacted areas, with MLCAPE in excess of 4000
   J/kg expected for parts of north-central/northeast Texas into far
   southeast Oklahoma and the nearby ArkLaTex. Special 19Z observed
   soundings are expected from Fort Worth/Shreveport to help better
   gauge/confirm this destabilization trend and the degree of capping.

我尝试使用以下代码,但仅显示...SUMMARY...和下一行。

sed -n '/...SUMMARY.../,/.../p' 

该怎么办?

================================================ ======================== 后续行动:

这是我想要得到的结果。只显示... SUMMARY ...下的段落,然后在下一个...结束,这就是我应该得到的:

中北部/东北部最可能发生雷暴    下午晚些时候和    晚间。破坏性的冰雹和风,以及一些龙卷风    可能。预计在整个    中南部和俄亥俄州山谷。

我已根据Shellter的建议尝试了以下方法:

sed -n'/ ... SUMMARY ... /,/ ** ... ** / p'

但是我什么都有。

1 个答案:

答案 0 :(得分:1)

您可以使用

sed -n '/^[[:blank:]]*\.\.\.SUMMARY\.\.\./,/^[[:blank:]]*\.\.\./{//!p;}' file

请参见this online sed demo

注释