如何从包含许多相似模式的行中获取每个模式?(how to grep each pattern from a line consisting many alike patterns?)

编程入门 行业动态 更新时间:2024-10-23 22:22:37
如何从包含许多相似模式的行中获取每个模式?(how to grep each pattern from a line consisting many alike patterns?)

假设我有一行如下:

blah blah. Jon abc efg Don. blah blah. Jon afjfa kadkca Don.blah blah.

现在我想得到的模式是“Jon”作为第一个单词,“Don”作为最后一个单词,中间没有“Jon”或“Don”。这意味着我想:

Jon abc efg Don Jon afjfa kadkca Don

首先,我试过了

egrep -o 'Jon.*Don'

但它表明

Jon abc efg Don. blah blah. Jon afjfa kadkca Don

所以我试图从模式的中间排除“Don”

egrep -o 'Jon.*[^(Don)]Don'

但它也没有用。

suppose I have a line like the following one:

blah blah. Jon abc efg Don. blah blah. Jon afjfa kadkca Don.blah blah.

Now I want to get patterns with "Jon" as the first word and "Don" as the last one, with no "Jon" or "Don" in between.That means I want:

Jon abc efg Don Jon afjfa kadkca Don

Firstly, I tried

egrep -o 'Jon.*Don'

But it shows

Jon abc efg Don. blah blah. Jon afjfa kadkca Don

So I tried to exclude "Don" from middle of the pattern

egrep -o 'Jon.*[^(Don)]Don'

But it didn't work either.

最满意答案

你可以试试这个,

grep -oP 'Jon.*?Don' file

egrep是折旧的,与grep -E相同。 这不会支持非贪心量词。 您需要使用-P perl-regexp参数来实现非贪婪的形式。

You may try this,

grep -oP 'Jon.*?Don' file

egrep is depreciated which is same as grep -E . This won't support non-greedy quantifier. You need to use -P perl-regexp parameter to achieve the non-greedy form.

更多推荐

本文发布于:2023-08-03 12:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   pattern   grep   line   alike

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!