pdfファイルを直接grepできるpdfgrep

Terminal Trove でpdfgrep というツールを知ったので試してみました.

pdfgrep はpdf ファイルをgrep と同様パターンにマッチする行を表示してくれるものです.オプションは GNU grep をある程度踏襲しています.

Debian sid 環境でsource からbuild する例
$ sudo apt install libgcrypt20-dev libpoppler-cpp-dev
$ git clone https://gitlab.com/pdfgrep/pdfgrep
$ cd pdfgrep
$ ./autogen.sh
$ ./configure --prefix=$HOME/usr/local
$ make
$ make install
help
$ pdfgrep --help
Usage: ./src/pdfgrep [OPTION]... PATTERN FILE...

Search for PATTERN in each FILE.
PATTERN is, by default, an extended regular expression.

Commonly used options:
 -i, --ignore-case              Ignore case distinctions
 -P, --perl-regexp              Use Perl compatible regular expressions (PCRE)
 -H, --with-filename            Print the file name for each match
 -h, --no-filename              Suppress the prefixing of file name on output
 -n, --page-number              Print page number with output lines
 -c, --count                    Print only a count of matches per file
     --color WHEN               Use colors for highlighting;
                                WHEN can be `always', `never' or `auto'
 -p, --page-count               Print only a count of matches per page
 -m, --max-count NUM            Stop reading after NUM matching lines (per file)
 -q, --quiet                    Suppress normal output
 -r, --recursive                Search directories recursively
 -R, --dereference-recursive    Likewise, but follow all symlinks
     --cache                    Use cache for faster operation
     --help                     Print this help
 -V, --version                  Show version information

The above list is only a selection of commonly used options. Please refer
to the man page for a complete list.
適当なpdf ファイルをgrep
$ pdfgrep raspberry ~/Downloads/MagPi128.pdf | head
raspberrypi.com
                       magpi@raspberrypi.com
                       forums.raspberrypi.com
                                                     lucy@raspberrypi.com
                                                     rob@raspberrypi.com
                                                     charlotte.milligan@raspberrypi.com
                                                     brian.jepson@raspberrypi.com
 Learn Sense HAT             magpi@raspberrypi.com             (CC BY-NC-SA 3.0).
GNU grep でよく使うオプションを利用
$ pdfgrep -m5 -n -i raspberry ~/Downloads/MagPi128.pdf
1:Issue 128     April 2023   magpi.cc    The official Raspberry Pi magazine
1:   Discover coding with Raspberry Pi
1:            NEW! RASPBERRY PI GLOBAL SHUTTER CAMERA
2: Industrial Raspberry Pi
3:(page 44) can help you out on this journey. I hope you take it.                 Raspberry Pi office
                                     This Month in Raspberry Pi
日本語もok
$ pdfgrep -r -m1 -n サーバー ~/Nextcloud_matoken\@files.matoken.org/Linux/2025/
/home/matoken/Nextcloud_matoken@files.matoken.org/Linux/2025//sakura-cominuty-2025/snac-kagolug.pdf:2:サーバー設定ファイルの server.json を編集します。 "protocol" を
/home/matoken/Nextcloud_matoken@files.matoken.org/Linux/2025//kagolug2025.08/slide/slide.pdf:1:  ルサーバーの
/home/matoken/Nextcloud_matoken@files.matoken.org/Linux/2025//kagolug2025.09/slide/slide.pdf:19:ActivityPubも少し探したが,自分とサーバーの落ちているものしか
/home/matoken/Nextcloud_matoken@files.matoken.org/Linux/2025//kagolug2025.09/slide/matoken-slide.pdf:19:ActivityPubも少し探したが,自分とサーバーの落ちているものしか
/home/matoken/Nextcloud_matoken@files.matoken.org/Linux/2025//kagolug2025.10/slide/slide.pdf:10:     2     サーバー設定ファイルを編集."protocol" を "http"

オプションの詳細は以下を.

これまではpdf ファイルをpdftotext などでテキストに変換しつつgrep などしていましたがこれだとどのページ化が分かりづらかったです.pdfgrep だと -n オプションが行数ではなくページ数になるのでこのあたりがわかりやすくなって良さそうです.

$ pdftotext ~/Downloads/RetroBSD_and_LiteBSD.pdf - | grep -m5 -n -i retro
1:RetroBSD and LiteBSD:
15:  Contributing to RetroBSD since 2013
30:  RetroBSD: an OS from the past in 128k RAM
35:
   RetroBSD and LiteBSD: an overview
39:  RetroBSD: PIC32MX
$ pdfgrep -m5 -n -i retro ~/Downloads/RetroBSD_and_LiteBSD.pdf
1:RetroBSD and LiteBSD:
3:  Contributing to RetroBSD since 2013
4:  RetroBSD: an OS from the past in 128k RAM
5:RetroBSD and LiteBSD: an overview
5:     RetroBSD: PIC32MX

と,一通り遊んでから気づいたのですが,debian にはpdfgrep パッケージが結構前から存在していました.

$ apt-cache search pdfgrep
pdfgrep - 正規表現にマッチする文字列を pdf ファイル内で検索
$ rmadison pdfgrep
pdfgrep    | 2.1.2-1       | oldoldstable   | source, amd64, arm64, armhf, i386
pdfgrep    | 2.1.2-1       | oldstable      | source, amd64, arm64, armel, armhf, i386, mips64el, mipsel, ppc64el, s390x
pdfgrep    | 2.2.0-0.1     | stable         | source
pdfgrep    | 2.2.0-0.1     | testing        | source
pdfgrep    | 2.2.0-0.1     | unstable       | source
pdfgrep    | 2.2.0-0.1     | unstable-debug | source
pdfgrep    | 2.2.0-0.1+b2  | stable         | amd64, arm64, armel, armhf, i386, ppc64el, riscv64, s390x
pdfgrep    | 2.2.0-0.1+b2  | testing        | amd64, arm64, armhf, i386, ppc64el, riscv64, s390x
pdfgrep    | 2.2.0-0.1+b2  | unstable       | amd64, arm64, armhf, i386, ppc64el, riscv64, s390x
$ sudo apt install pdfgrep
$ /usr/bin/pdfgrep -V
This is pdfgrep version 2.2.0.

Using poppler version 25.03.0
Using libpcre2 version 10.46 2025-08-27
環境
$ pdfgrep -V
This is pdfgrep version 2.2.0.

Using poppler version 25.03.0
Using libpcre2 version 10.46 2025-08-27
Built from git-commit v2.2.0-7-gaa71955
$ /usr/bin/pdfgrep -V
This is pdfgrep version 2.2.0.

Using poppler version 25.03.0
Using libpcre2 version 10.46 2025-08-27
$ dpkg-query -W poppler-utils
poppler-utils   25.03.0-11.1+b1
$ pdftotext -v
pdftotext version 25.03.0
Copyright 2005-2025 The Poppler Developers - http://poppler.freedesktop.org
Copyright 1996-2011, 2022 Glyph & Cog, LLC
$ lsb_release -dr
Description:    Debian GNU/Linux forky/sid
Release:        n/a
$ arch
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です