If you want to avoid having to decompress the file again and again for each pattern, you could do:
PATTERNS='foo
bar
baz' find . -mtime -"$a" -type f ! -name "*.bak*" -exec awk -v q=\' '
function shquote(s) {
gsub(q, q "\\" q q, s)
return q s q
}
BEGIN {
n = split(ENVIRON["PATTERNS"], pats, "\n")
for (arg = 1; arg < ARGC; arg++) {
file = ARGV[arg]
cmd = "gzip -dcf < " shquote(file)
for (i = 1; i <= n; i++) notfound[pats[i]]
left = n
while (left && (cmd | getline line) > 0) {
for (pat in notfound) {
if (line ~ pat) {
if (!--left) {
print file
break
}
delete notfound[pat]
}
}
}
close(cmd)
}
exit
}' {} +
Note that the patterns are taken as awk
patterns, that's similar to the extended regular expressions supported by grep -E
/egrep
. For case insensitive matching, you can add a -v IGNORECASE=1
if using GNU awk
, or portably change to:
PATTERNS='foo
bar
baz' find . -mtime -"$a" -type f ! -name "*.bak*" -exec awk -v q=\' '
function shquote(s) {
gsub(q, q "\\" q q, s)
return q s q
}
BEGIN {
n = split(tolower(ENVIRON["PATTERNS"]), pats, "\n")
for (arg = 1; arg < ARGC; arg++) {
file = ARGV[arg]
cmd = "gzip -dcf < " shquote(file)
for (i = 1; i <= n; i++) notfound[pats[i]]
left = n
while (left && (cmd | getline line) > 0) {
line = tolower(line)
for (pat in notfound) {
if (line ~ pat) {
if (!--left) {
print file
break
}
delete notfound[pat]
}
}
}
close(cmd)
}
exit
}' {} +
(assuming the patterns don't have non-standard ERE extensions like \S
, which would be converted to \s
).
You could put that awk
command in a zgrep-many
script to make it easier to use. Something like:
#! /bin/sh -
usage() {
cat >&2 << EOF
Usage: $0 [-e <pattern>] [-f <file] [-i] [pattern] files
List the files for which all the given patterns are matched.
EOF
exit 1
}
ignorecase=
PATTERNS=
export PATTERNS
NL='
'
sep=
while getopts e:f:i opt; do
case $opt in
(e) PATTERNS=$PATTERNS$sep$OPTARG; sep=$NL;;
(f) PATTERNS=$PATTERNS$sep$(cat < "$OPTARG") || exit; sep=$NL;;
(i) ignorecase='tolower(';;
(*) usage;;
esac
done
shift "$((OPTIND - 1))"
if [ -z "$PATTERNS" ]; then
[ "$#" -gt 0 ] || usage
PATTERN=$1; shift
fi
[ "$#" -eq 0 ] && exit
exec awk -v q=\' '
function shquote(s) {
gsub(q, q "\\" q q, s)
return q s q
}
BEGIN {
n = split('"$ignorecase"'ENVIRON["PATTERNS"]'"${ignorecase:+)}"', pats, "\n")
for (arg = 1; arg < ARGC; arg++) {
file = ARGV[arg]
cmd = "gzip -dcf < " shquote(file)
for (i = 1; i <= n; i++) notfound[pats[i]]
left = n
while (left && (cmd | getline line) > 0) {
'"${ignorecase:+line = tolower(line)}"'
for (pat in notfound) {
if (line ~ pat) {
if (!--left) {
print file
break
}
delete notfound[pat]
}
}
}
close(cmd)
}
exit
}' "$@"
To be used as:
find ... -exec zgrep-many -ie foo -e bar -e baz {} +
for instance.
find
search. – Stéphane Chazelas Aug 24 '17 at 13:03