对逻辑 OR 条件使用正则表达式替换
如果您尝试查找包含“myFunction”或“myClass”的文件,您可以使用带有交替的扩展正则表达式 例如:
# Using GNU Find and GNU Grep
find . exec grep --extended-regexp --files-with-matches 'myFunction|myClass' {} +
当将文件列表传递给 grep 时,这将显示包含任一单词的匹配文件。
逻辑与更棘手
逻辑 AND 比较棘手,因为您必须考虑排序。您可以:
- 根据一组要求过滤文件,然后是另一组。
- 使用功能更全的程序来存储状态。
作为第一种情况的一个简单示例:
# Use nulls to separate filenames for safety.
find /etc/passwd -print0 |
xargs -0 egrep -Zl root |
xargs -0 egrep -Zl www
作为第二种情况的人为示例,您可以使用 GNU awk:
# Print name of current file if it matches both alternates
# on different lines.
find /etc/passwd -print0 |
xargs -0 awk 'BEGIN {matches=0};
/root|www/ {matches+=1};
matches >= 2 {print FILENAME; matches=0; nextfile}'