Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 显示grep不在的文件';没有找到?_Bash_Shell_Command Line_Grep - Fatal编程技术网

Bash 显示grep不在的文件';没有找到?

Bash 显示grep不在的文件';没有找到?,bash,shell,command-line,grep,Bash,Shell,Command Line,Grep,以下是我当前的代码: grep -o -P '(?<=class\=\"name\">).*?(?=<\/div>)' * > ../name.txt 我希望它输出如下: file1.html:Bob file3.html:Person file4.html:Other file1.html:Bob file2.html:UNKNOWN file3.html:Person file4.html:Other 这是可能的还是我遗漏了什么?您必须编写一个循环来报告不

以下是我当前的代码:

grep -o -P '(?<=class\=\"name\">).*?(?=<\/div>)' * > ../name.txt
我希望它输出如下:

file1.html:Bob
file3.html:Person
file4.html:Other
file1.html:Bob
file2.html:UNKNOWN
file3.html:Person
file4.html:Other

这是可能的还是我遗漏了什么?

您必须编写一个循环来报告不匹配的文件

for file in *; do
    grep -h -o -P '(?<=class\=\"name\">).*?(?=<\/div>)' "$file" || echo "$file:UNKNOWN"
done
用于*中的文件;做
grep-h-o-P'(?).*(?=)'“$file”| | echo“$file:UNKNOWN”
完成

P
似乎不是
grep
标志。@JackHasaKeyboard是一个非标准标志,表示使用PCRE引擎。@JackHasaKeyboard:在Mac上,
-P
选项不存在。。。