我有一个文件,我想从该文件中 grep 一个特定的单词。我正在使用 MobaXterm。例如,我想 grep 单词 test-common 并且在我的文件中我有
1. Version: 1.0.0 test-common Author Created Path
2. Version: 1.0.0 test-common-config Author Created Path
3. Version: 1.0.0 unit-test-common Author Created Path
4. Version: 1.0.0 unit-test-common-config Author Created Path
我尝试过:
-
grep test-common file
-
grep "\\< test-common \\>" file
(不返回任何内容) -
grep -w test-common file
-
grep -w "test-common " file
-
grep -r "\btest-common\b" file
(不返回任何内容) -
grep -sw test-common file
(不起作用) -
grep -o test-common file
(不起作用) -
grep \\< bil-common \\>
文件
还有^bil-common
和bil-common$
也不工作。
对于我得到结果的命令,结果是:
test-common
test-common-config
这不是我想要的。如有任何帮助,我们将不胜感激。
最佳答案
你可以这样做:
grep -E '(^|\s+)test-common(?=\s|$)' file
这匹配“test-common”,后面仅跟一个空格或位于行尾,前面有一个或多个空格,或者位于行首。
关于linux - Grep UNIX 精确单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48607611/