假设您有一个名为abc.txt的文件 - 该文件包含2行(或通常更多行):
word -c (09:35:20)
word -c (09:38:49)
Run Code Online (Sandbox Code Playgroud)
如果运行该命令$ grep "word -c" abc.txt,则只获得第1行,因为1和-c之间的空格数与第2行不匹配.有没有办法解决这个问题?
你不能使用grep'word1|word2' /path/to/fileword和-c之间的空格.
使用+正则表达式字符,它至少匹配前面一个字符:
grep -E "word +-c" abc.txt
Run Code Online (Sandbox Code Playgroud)
这个正则表达式是"匹配'单词',后跟一个或多个空格,后跟'-c'."
grep 'word \+-c' abc.txt
使用单引号,而不是双引号。“+”表示“一个或多个前面的表达式,因此可以找到任意数量的空格(除了零)。
这是一个参考:http://www.gnu.org/software/findutils/manual/html_node/find_html/grep-regular-expression-syntax.html