linux - Grep UNIX 精确单词

标签 linux bash shell unix grep

我有一个文件,我想从该文件中 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-commonbil-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/

相关文章:

bash - 如何在 bash 中将字符串转换为 int?

bash - 在shell脚本中放置elasticsearch curl命令时奇怪的输出

bash - 如何将 time 命令的输出重定向到日志文件?

linux - pthread_setaffinity_np : Invalid argument?

c++ - 获取 ibus 设置的实际键码

php - Mongodb安装

python - Linux start-stop-daemon目录错误调用shell/python脚本

bash - 从某些文件读取时连接字符串失败

shell - 检查今天是否是某个日期

bash - 即使状态代码为 400,如何从 curl 请求输出到文件?