如何使用 pdfgrep 从终端搜索 PDF 文件
pdfgrep pdf
![](/data/attachment/album/201801/16/132004ioiyi5n78n58ziin.jpg)


诸如 [grep](https://www.maketecheasier.com/what-is-grep-and-uses/) 和 [ack-grep](https://www.maketecheasier.com/ack-a-better-grep/) 之类的命令行工具对于搜索匹配指定[正则表达式](https://www.maketecheasier.com/the-beginner-guide-to-regular-expressions/)的纯文本非常有用。但是你有没有试过使用这些工具在 PDF 中搜索？不要这么做！由于这些工具无法读取PDF文件，因此你不会得到任何结果。它们只能读取纯文本文件。


顾名思义，[pdfgrep](https://pdfgrep.org/) 是一个可以在不打开文件的情况下搜索 PDF 中的文本的小命令行程序。它非常快速 —— 比几乎所有 PDF 浏览器提供的搜索更快。`grep` 和 `pdfgrep` 的最大区别在于 `pdfgrep` 对页进行操作，而 `grep` 对行操作。`grep` 如果在一行上找到多个匹配项，它也会多次打印单行。让我们看看如何使用该工具。


### 安装


对于 Ubuntu 和其他基于 Ubuntu 的 Linux 发行版来说，这非常简单：



```
sudo apt install pdfgrep

```

对于其他发行版，只要在[包管理器](https://www.maketecheasier.com/install-software-in-various-linux-distros/)里输入 “pdfgrep” 查找，它就应该能够安装它。万一你想浏览其代码，你也可以查看项目的 [GitLab 页面](https://gitlab.com/pdfgrep/pdfgrep)。


### 测试运行


现在你已经安装了这个工具，让我们去测试一下。`pdfgrep` 命令采用以下格式：



```
pdfgrep [OPTION...] PATTERN [FILE...]

```

* `OPTION` 是一个额外的属性列表，给出诸如 `-i` 或 `--ignore-case` 这样的命令，这两者都会忽略匹配正则中的大小写。
* `PATTERN` 是一个扩展正则表达式。
* `FILE` 如果它在相同的工作目录就是文件的名称，或文件的路径。


我对 Python 3.6 官方文档运行该命令。下图是结果。


![pdfgrep search](/data/attachment/album/201801/16/132005kwnwdjgo6d76wigi.png "pdfgrep search")


红色高亮显示所有遇到单词 “queue” 的地方。在命令中加入 `-i` 选项将会匹配单词 “Queue”。请记住，当加入 `-i` 时，大小写并不重要。


### 其它


`pdfgrep` 有相当多的有趣的选项。不过，我只会在这里介绍几个。


* `-c` 或者 `--count`：这会抑制匹配的正常输出。它只显示在文件中遇到该单词的次数，而不是显示匹配的长输出。
* `-p` 或者 `--page-count`：这个选项打印页面上匹配的页码和页面上的该匹配模式出现次数。
* `-m` 或者 `--max-count` [number]：指定匹配的最大数目。这意味着当达到匹配次数时，该命令停止读取文件。


所支持的选项的完整列表可以在 man 页面或者 `pdfgrep` 在线[文档](https://pdfgrep.org/doc.html)中找到。如果你在批量处理一些文件，不要忘记，`pdfgrep` 可以同时搜索多个文件。可以通过更改 `GREP_COLORS` 环境变量来更改默认的匹配高亮颜色。


### 总结


下一次你想在 PDF 中搜索一些东西。请考虑使用 `pdfgrep`。该工具会派上用场，并且节省你的时间。




---


via: <https://www.maketecheasier.com/search-pdf-files-pdfgrep/>


作者：[Bruno Edoh](https://www.maketecheasier.com) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Pdfgrep](https://www.maketecheasier.com/assets/uploads/2017/11/pdfgrep-words.jpg)

Command line utilities such as [grep](https://www.maketecheasier.com/what-is-grep-and-uses/) and [ack-grep](https://www.maketecheasier.com/ack-a-better-grep/) are great for searching plain-text files for patterns matching a specified [regular expression](https://www.maketecheasier.com/the-beginner-guide-to-regular-expressions/). But have you ever tried using these utilities to search for patterns in a PDF file? Well, don’t! You will not get any result as these tools cannot read PDF files; they only read plain-text files.

[pdfgrep](https://pdfgrep.org/), as the name suggests, is a small command line utility that makes it possible to search for text in a PDF file without opening the file. It is insanely fast – faster than the search provided by virtually all PDF document viewers. A great distinction between grep and pdfgrep is that pdfgrep operates on pages, whereas grep operates on lines. It also prints a single line multiple times if more than one match is found on that line. Let’s look at how exactly to use the tool.

## Installation

For Ubuntu and other Linux distros based on Ubuntu, it is pretty simple:

sudo apt install pdfgrep

For other distros, just provide `pdfgrep`

as input for the [package manager](https://www.maketecheasier.com/install-software-in-various-linux-distros/), and that should get it installed. You can also check out the project’s [GitLab page](https://gitlab.com/pdfgrep/pdfgrep), in case you want to play around with the code.

## The Test Run

Now that you have the tool installed, let’s go for a test run. pdfgrep command takes this format:

pdfgrep [OPTION...] PATTERN [FILE...]

**OPTION** is a list of extra attributes to give the command such as `-i`

or `--ignore-case`

, which both ignore the case distinction between the regular pattern specified and the once matching it from the file.

**PATTERN** is just an extended regular expression.

**FILE** is just the name of the file, if it is in the same working directory, or the path to the file.

I ran the command on Python 3.6 official documentation. The following image is the result.

The red highlights indicate all the places the word “queue” was encountered. Passing `-i`

as option to the command included matches of the word “Queue.” Remember, the case does not matter when `-i`

is passed as an option.

## Extras

pdfgrep has quite a number of interesting options to use. However, I’ll cover only a few here.

`-c`

or`--count`

: this suppresses the normal output of matches. Instead of displaying the long output of the matches, it only displays a value representing the number of times the word was encountered in the file`-p`

or`--page-count`

: this option prints out the page numbers of matches and the number of occurrences of the pattern on the page`-m`

or`--max-count`

[number]: specifies the maximum number of matches. That means when the number of matches is reached, the command stops reading the file.

The full list of supported options can be found in the man pages or in the pdfgrep online [documenation](https://pdfgrep.org/doc.html). Don’t forget pdfgrep can search multiple files at the same time, in case you’re working with some bulk files. The default match highlight color can be changed by altering the GREP_COLORS environment variable.

## Conclusion

The next time you think of opening up a PDF file to search for anything. think of using pdfgrep. The tool comes in handy and will save you time.

Our latest tutorials delivered straight to your inbox