こんにちは
pdftotextコマンドを用い、pdfファイルのページ単位で一行のテキストへ変換するシェルスクリプトを作りました。
これを用い、指定した単語の検索を行い、それが出現するページ番号を得ました。
$ ./pdftotext.sh example.pdf | grep -n -e "search_word" | cut -d: -f1
1
2
$
pdftotext.sh
# !/bin/sh
# constant
FF=$(printf '\f')
# functions
remove_form_feed_f() {
str="${1#$FF}"
echo "$str"
[ "$str" != "$1" ]; return $?
}
pdftotext_f() {
file="$1"
pdftotext "$file" - | while read -r line; do
line=$(remove_form_feed_f "$line") && echo
printf "%s" "$line"
done
echo
}
# main
for file in "$@"; do
[ -d "$file" ] || [ "${file##*.}" != "pdf" ] && continue
pdftotext_f "$file"
done