如何匹配PATTERN但不匹配PREFIX_PATTERN? [英] How do I match PATTERN but not PREFIX_PATTERN?
问题描述
假设我有一些包含 KEYWORD
的几行文件,以及一些带有 PREFIX_KEYWORD
的行。 p>
如何仅匹配具有 KEYWORD
的这些行,并忽略具有 PREFIX_KEYWORD
在它们上面?
是的,我可以grep for KEYWORD
,将结果输入编辑器,并让编辑器删除所有含有 PREFIX_KEYWORD
的行,但我问grep中是否有内置的方法来执行此操作。
如果这有帮助:我对完全匹配不感兴趣,但只想知道文件中是否存在 KEYWORD
。
一种方法是grep您的KEYWORD并过滤掉其余的。这可能看起来像
grep KEYWORD文件| grep -v PREFIX_KEYWORD
使用perl表达式的另一种方式:
grep -P'(?<!PREFIX_)KEYWORD'文件
同样的答案在这里给出:正则表达式匹配没有给定前缀的特定字符串
Say I've got some documents that contain several lines with KEYWORD
and some lines with PREFIX_KEYWORD
.
How would I match only these lines that have KEYWORD
and ignore the lines that have PREFIX_KEYWORD
on them?
Yes, I could grep for KEYWORD
, feed the results into the editor of my choice and let the editor delete all lines that have PREFIX_KEYWORD
but I'm asking whether there's a built-in way in grep to do this.
If this helps: I'm not interested in the exact match but only want to know whether there are occurrences of KEYWORD
in the file.
One way would be to grep for your KEYWORD and filter out the rest. This could look like
grep KEYWORD file | grep -v PREFIX_KEYWORD
Another way with a perl expression:
grep -P '(?<!PREFIX_)KEYWORD' file
The same answer was given here: Regex to match specific strings without a given prefix
这篇关于如何匹配PATTERN但不匹配PREFIX_PATTERN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!