【发布时间】:2017-12-14 20:22:12
【问题描述】:
这是我当前的代码:
grep -o -P '(?<=class\=\"name\">).*?(?=<\/div>)' * > ../name.txt
现在这是搜索目录中的每个文件并输出介于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
这是可能的还是我错过了什么?
【问题讨论】:
-
P似乎不是grep标志。 -
@JackHasaKeyboard 这是一个非标准标志,表示使用PCRE引擎。
-
@JackHasaKeyboard:在 Mac 上,
-P选项不存在...
标签: bash shell command-line grep