Skip to content

Instantly share code, notes, and snippets.

@Kagee
Last active August 30, 2024 22:20
Show Gist options
  • Save Kagee/8767b2108983986a0af56587d3b6c922 to your computer and use it in GitHub Desktop.
Save Kagee/8767b2108983986a0af56587d3b6c922 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
if [[ -z $1 ]]; then
printf "Usage: %s: <grep arguments> [filename]\n" "$(basename "$0")"
exit 1
fi
function _grep {
PRINT_FILENAME="$1"
FILENAME="$2"
BASENAME="$(basename "$2" .epub)"
MATCHES=1
for FILENAME_IN_ZIP in $(unzip -Z1 "$FILENAME" | grep -P '.*\.html'); do
if (unzip -p-L "$FILENAME" "$FILENAME_IN_ZIP" | \
sed -e 's/<[^>]*>//g' | \
sed -e '/^[[:space:]]*$/d' | \
sed -e 's/^[[:space:]]*//g' | \
while IFS= read -r line; do
if $PRINT_FILENAME; then
echo -n -e "\e[31m$BASENAME\e[0m: ";
fi
echo "$line";
done) | grep "${@:3}"; then
MATCHES=0
fi
done
return $MATCHES
}
if [[ -f "${@:$#}" ]]; then
_grep "false" "${@:$#}" "${@:1:$(($#-1))}";
exit $?
fi
FIRST=true
while IFS='' read -r -d '' FILENAME; do
OUTPUT=$(_grep "true" "$FILENAME" "$@";)
if [[ $? == 0 ]]; then
if ! $FIRST; then
echo
fi
echo "$OUTPUT"
else
echo -n .
fi
FIRST=false
done < <(find . -type f -name '*.epub' -print0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment