我正在尝试读取文件并使用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'
但是我什么都有。
答案 0 :(得分:1)
您可以使用
sed -n '/^[[:blank:]]*\.\.\.SUMMARY\.\.\./,/^[[:blank:]]*\.\.\./{//!p;}' file
注释:
/.../
仅与任何3个字符匹配的行^
匹配行的开头,[[:space:]]*
匹配任何0+个空格字符{//!p;}
使您获得除这两行以外的两行之间的内容(请参阅How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?)