Last active
December 15, 2023 01:17
-
-
Save ngregoire/8c5a569f9c56573f70856028b59d844f to your computer and use it in GitHub Desktop.
Grep through PDF files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Three arguments: ROOT_DIR, PATTERN, OPTIONS | |
# Search below $ROOT_DIR for PDF files matching $PATTERN | |
# $OPTIONS is passed to pdfgrep (ex: grep-pdf . 'some words' -h -C5) | |
# ROOT_DIR | |
if [ -z "$1" ]; then | |
echo "! Argument ROOT_DIR is needed!" | |
exit 1 | |
else | |
ROOT_DIR="$1" | |
fi | |
# PATTERN | |
if [ -z "$2" ]; then | |
echo "! Argument PATTERN is needed!" | |
exit 1 | |
else | |
PATTERN=$2 | |
fi | |
# OPTIONS | |
OPTIONS="-H -n --warn-empty" | |
if [ -z "$3" ]; then | |
true | |
else | |
# Pass all remaining arguments | |
shift | |
shift | |
OPTIONS="$OPTIONS $@" | |
fi | |
# DO THE STUFF | |
echo "# Searching for '${PATTERN}' in PDF files below $ROOT_DIR (options '$OPTIONS')" | |
find $ROOT_DIR -name "*.pdf" -print0 | xargs -0 pdfgrep $OPTIONS "$PATTERN" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment