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/