我试图在ubuntu中使用grep在名为test的文件中搜索模式
以下是测试内容
./foldera/[hello]this.mp4
./foldera/folderb/[hello]that.mp4
./folderc/[these]hello.mp4
在这个网站regexp simulator上,我使用以下模式进行搜索并且它有效,三行匹配。
.*\/[A-Za-z0-9\[\]]+\.mp4
但是在ubuntu中,我在终端中运行了以下命令,它不起作用,终端没有返回任何内容。
timothy@ubuntu:~$ cat ~/Desktop/test
./foldera/[hello]this.mp4
./foldera/folderb/[hello]that.mp4
./folderc/[these]hello.mp4
timothy@ubuntu:~$ cat ~/Desktop/test | grep -E '.*\/[A-Za-z0-9\[\]]+\.mp4'
timothy@ubuntu:~$
grep无法搜索文件中的所有行的原因是什么?
答案 0 :(得分:0)
grep
扩展正则表达式不使用反斜杠来转义方括号内的方括号。正确的方法是将]
作为方括号中的第一个字符;这被视为文字字符,因为你不能有空字符集。
grep -E '/[]A-Za-z0-9[]+\.mp4' test.txt
一开始也不需要.*
。 grep
只是检查该行上的任何内容是否与模式匹配,因此在开头或结尾添加匹配项是多余的(仅当您使用-o
打印部分内容时才需要匹配的行。)