linux - 如果破折号在字符串中,则 grep -w 不是唯一的

标签 linux bash grep

i如果破折号在字符串中“grep -w”不是唯一的。我该如何解决这个问题?

示例:

文件1:

football01
football01test

# grep -iw ^football01
football01

文件 2:

football01
football01-test

# grep -iw ^football01
football01
football01-test

最佳答案

这是预期和记录的行为:

   -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.

如果您添加破折号,它会终止您的第一个单词,因为破折号是“非单词组成字符”。如果你把这些词写在一起,那么 word-regexp grep 会把它当作一个词而不匹配它。

你到底想做什么?

如果你只想知道你的台词是不是 football01 而没有别的,你可以这样做

grep -i "^football01$"

如果您想实现其他目标,请您解释一下它是什么。

关于linux - 如果破折号在字符串中,则 grep -w 不是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46472771/

相关文章:

linux - 我在 chrome 中删除了 cookies 文件,但在我重新启动 chrome 后我仍然登录

bash - 在kubernetes pod中执行命令(bash脚本)

linux - 从文件中读取日期时间并添加一秒

grep 中的正则表达式先行查找 'not followed by'

search - grep 搜索查找。仅在具有 .txt 扩展名的文件中

php - 正则表达式获取前后N个词

linux - 用于检查进程的 Shell 脚本

c++ - 大内存块的快速内存分配

c++ - 链接到 .a,但仍然需要 .so? (C++, Linux)

Linux bash 脚本,用于在满足条件的情况下查找并删除目录树中具有特殊字符和空格的最旧文件