[英]Find exact word in file using egrep in bash
I have a file called F1
. 我有一个名为
F1
的文件。 F1
contains F1
包含
Hello abc@ aaa ddd
Now, I want to check the word abc
is in file F1
. 现在,我想检查单词
abc
在文件F1
。
After running the following command 运行以下命令后
find $dir -type f -exec egrep -w "abc" {} \;
This is the output I get 这是我得到的输出
Hello abc@
For some reason the word abc@
was also found. 由于某种原因,还找到了单词
abc@
。 (I was looking for abc
.) (我正在寻找
abc
。)
This should do it: 应该这样做:
egrep '(^| )abc( |$)' F1
It looks for abc
surrounded by either space or line beginning/ending. 它查找由空格或行首/结尾包围的
abc
。
You're likely using GNU egrep - the -w
option isn't part of the POSIX standard - and its manual page states 您可能正在使用GNU egrep--
-w
选项不是POSIX标准的一部分-及其手册页状态
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
So @
is considered a non-word constituent character, just like ,
and .
所以
@
被认为是一种非单词组成的性格,就像,
和.
and whitespace. 和空格。
If you have an idea of what you'd like the word separators to be, let us know and we can craft a regexp for you. 如果您对分隔符有什么想法,请告诉我们,我们可以为您制作一个正则表达式。
find $dir -type f -exec grep -v [[:punct:]] {} \; | grep -ow abc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.