【问题标题】:Show files where grep wasn't found?显示找不到 grep 的文件?
【发布时间】:2017-12-14 20:22:12
【问题描述】:

这是我当前的代码:

grep -o -P '(?<=class\=\"name\">).*?(?=<\/div>)' * > ../name.txt

现在这是搜索目录中的每个文件并输出介于class="name"&gt;&lt;/div&gt; 之间的名称。

但如果在文件中找不到任何内容,则该文件根本不会出现在输出的 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


【解决方案1】:

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

for file in *; do
    grep -h -o -P '(?<=class\=\"name\">).*?(?=<\/div>)' "$file" || echo "$file:UNKNOWN"
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多