【发布时间】:2019-11-02 12:30:45
【问题描述】:
我正在尝试读取文件并使用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... 下的段落并在下一个...结束,所以这就是我最后应该得到的:
中北部/东北部最有可能出现强雷暴 德克萨斯州和 Ark-La-Tex 地区在下午晚些时候和 晚上。破坏性的冰雹和大风,以及一些龙卷风 可能的。预计全境还会有强雷暴 中南部和俄亥俄河谷。
我根据 Shellter 的推荐尝试了以下方法:
sed -n '/...SUMMARY.../,/**...**/p'
但我得到了一切。
【问题讨论】:
-
.是正则表达式中的特殊字符。见:The Stack Overflow Regular Expressions FAQ -
运行以下命令,我得到以下结果: sed -n '/...SUMMARY.../,/^.../p' dspc ...SUMMARY... 严重雷暴最有可能横跨中北部/东北部所以它没有得到完整的措辞。
-
我认为您的
,/.../p与同一(第一)行匹配。我使用,/\*\*...\*\*/p'并在第一行之后得到了整个段落,但它也有下一段的第一行。您可以预先过滤您的文本,以便第二个哨兵在单独的行上,但我只会添加一个尾随过滤器来清除最后一行。好的第一个 Q,但如果您从提供的输入中包含预期的输出,它总是有助于减少歧义。祝你好运。 -
感谢 Shellter 效果很好,实际上也很好。我从 ...SUMMARY... 中得到所有内容,包括以下文本和其他所有内容。我正在寻找剥离 ...SUMMARY... 并仅显示下面的段落,然后没有其他内容。下面是我正在努力实现的目标,在下午晚些时候和晚上,德克萨斯州中北部/东北部和 Ark-La-Tex 地区很可能会出现严重的雷暴。破坏性的冰雹和大风,以及一些龙卷风是可能的。预计中南部和俄亥俄河谷也将出现强雷暴。