如何从包含许多相似模式的行中获取每个模式?(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 DonFirstly, I tried
egrep -o 'Jon.*Don'But it shows
Jon abc efg Don. blah blah. Jon afjfa kadkca DonSo 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' fileegrep是折旧的,与grep -E相同。 这不会支持非贪心量词。 您需要使用-P perl-regexp参数来实现非贪婪的形式。
You may try this,
grep -oP 'Jon.*?Don' fileegrep 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.
更多推荐
发布评论