如何搜索多个pdf文件的内容?
如何在目录/子目录中搜索 PDF 文件的内容?我正在寻找一些命令行工具。看来 grep
无法搜索 PDF 文件。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在目录/子目录中搜索 PDF 文件的内容?我正在寻找一些命令行工具。看来 grep
无法搜索 PDF 文件。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
有 pdfgrep,它的作用正如其名称所示。
我用它进行简单的搜索,效果很好。
(Debian、Ubuntu 和 Fedora 中都有软件包。)
从 1.3.0 版本起 pdfgrep 支持递归搜索。该版本自 Ubuntu 12.10 (Quantal) 起在 Ubuntu 中可用。
There is pdfgrep, which does exactly what its name suggests.
I've used it for simple searches and it worked fine.
(There are packages in Debian, Ubuntu and Fedora.)
Since version 1.3.0 pdfgrep supports recursive search. This version is available in Ubuntu since Ubuntu 12.10 (Quantal).
您的发行版应该提供一个名为
pdftotext
的实用程序:“-”是将 pdftotext 输出到 stdout 而不是文件所必需的。
--with-filename
和--label=
选项会将文件名放入 grep 的输出中。可选的
--color
标志很好,它告诉 grep 在终端上使用颜色进行输出。(在 Ubuntu 中,
pdftotext
由包xpdf-utils
或poppler-utils
提供。)此方法使用
pdftotext 和
grep
,如果您想使用grep
所不具备的 GNUgrep
功能,则比pdfgrep
有优势不支持。 注意:pdfgrep-1.3.x 支持-C
选项来打印上下文行。Your distribution should provide a utility called
pdftotext
:The "-" is necessary to have pdftotext output to stdout, not to files.
The
--with-filename
and--label=
options will put the file name in the output of grep.The optional
--color
flag is nice and tells grep to output using colors on the terminal.(In Ubuntu,
pdftotext
is provided by the packagexpdf-utils
orpoppler-utils
.)This method, using
pdftotext
andgrep
, has an advantage overpdfgrep
if you want to use features of GNUgrep
thatpdfgrep
doesn't support. Note: pdfgrep-1.3.x supports-C
option for printing line of context.Recoll 是一款出色的 Unix/Linux 全文 GUI 搜索应用程序,支持多种不同的格式,包括 PDF。它甚至可以将查询的确切页码和搜索词传递给文档查看器,从而允许您直接从其 GUI 跳转到结果。
Recoll 还附带了一个可行的命令行界面和一个网络浏览器界面。
Recoll is a fantastic full-text GUI search application for Unix/Linux that supports dozens of different formats, including PDF. It can even pass the exact page number and search term of a query to the document viewer and thus allows you to jump to the result right from its GUI.
Recoll also comes with a viable command-line interface and a web-browser interface.
我的 pdfgrep (1.3.0) 实际版本允许执行以下操作:
执行 pdfgrep --help 时:
它在我的 Ubuntu 上运行良好。
My actual version of pdfgrep (1.3.0) allows the following:
When doing
pdfgrep --help
:It works well on my Ubuntu.
还有另一个名为 ripgrep-all 的实用程序,它基于 ripgrep。
它不仅可以处理 PDF 文档,还可以处理 Office 文档和电影,作者 声称 它比
pdfgrep
更快。用于递归搜索当前目录的命令语法,第二个仅限于 PDF 文件:
There is another utility called ripgrep-all, which is based on ripgrep.
It can handle more than just PDF documents, like Office documents and movies, and the author claims it is faster than
pdfgrep
.Command syntax for recursively searching the current directory, and the second one limits to PDF files only:
我制作了这个破坏性小脚本。玩得开心。
I made this destructive small script. Have fun with it.
我喜欢 @sjr 的答案,但我更喜欢 xargs vs -exec。我发现 xargs 更通用。例如,使用 -P,我们可以在有意义的情况下利用多个 CPU。
I like @sjr's answer however I prefer xargs vs -exec. I find xargs more versatile. For example with -P we can take advantage of multiple CPUs when it makes sense to do so.
我遇到了同样的问题,因此我编写了一个脚本,该脚本在指定文件夹中的所有 pdf 文件中搜索字符串,并打印与查询字符串匹配的 PDF 文件。
也许这会对您有所帮助。
您可以在此处下载它
I had the same problem and thus I wrote a script which searches all pdf files in the specified folder for a string and prints the PDF files wich matched the query string.
Maybe this will be helpful to you.
You can download it here
如果您想使用 pdftotext 查看文件名,请使用以下命令:
If You want to see file names with pdftotext use following command:
首先将所有 pdf 文件转换为文本文件:
然后照常使用
grep
。这尤其好,因为当您有多个查询和大量 PDF 文件时它速度很快。First convert all your pdf files to text files:
Then use
grep
as normal. This is especially good as it is fast when you have multiple queries and a lot of PDF files.有一个开源公共资源 grep 工具 crgrep,它可以在 PDF 文件中搜索,还可以搜索其他资源,例如嵌套在其中的内容档案、数据库表、图像元数据、POM 文件依赖关系和 Web 资源 - 以及这些的组合,包括递归搜索。
“文件”选项卡下的完整描述几乎涵盖了该工具支持的内容。
我开发了 crgrep 作为开源工具。
There is an open source common resource grep tool crgrep which searches within PDF files but also other resources like content nested in archives, database tables, image meta-data, POM file dependencies and web resources - and combinations of these including recursive search.
The full description under the Files tab pretty much covers what the tool supports.
I developed crgrep as an opensource tool.
您需要一些工具,例如 pdf2text,首先将 pdf 转换为文本文件,然后在文本中进行搜索。 (您可能会错过一些信息或符号)。
如果您使用编程语言,可能有为此目的编写的 pdf 库。例如 http://search.cpan.org/dist/CAM-PDF/珀尔
You need some tools like pdf2text to first convert your pdf to a text file and then search inside the text. (You will probably miss some information or symbols).
If you are using a programming language there are probably pdf libraries written for this purpose. e.g. http://search.cpan.org/dist/CAM-PDF/ for Perl
尝试在像上面这样的简单脚本中使用“acroread”
try using 'acroread' in a simple script like the one above
感谢这里所有的好主意!
我尝试了 xargs 方法,但正如此处指出的,xargs 将使打印实际文件名变得不可能(或非常困难)......
所以我尝试了 GNU 并行。
--context=5
还打印上下5行以及上下文。-q
pdftotext 不会打印任何错误消息或警告(安静)。[]
作为标签,而不是大括号{}
。如果你想要大括号--label='{'{}'}'
就能实现。请注意,{}
被 GNU 并行替换为实际文件名,例如'Example portable document file name with paths.pdf'
({}
已经使用单引号'
)。--label={}
仅打印文件名,这可能是显示文件名的首选方式。--color=always
来强制输出。--ignore-case
添加到 grep 命令中以进行不区分大小写的关键字搜索可能很有用。如果要递归处理所有 PDF 文件,包括当前目录 (
.
) 中的所有子目录,则可以通过 find 来完成:-iname '*.pdf'< /code> 不区分大小写。使用
-name '*.pdf'
仅包含小写的 .pdf 文件(正常情况)。由于我有时也会遇到带有大写 .PDF 文件扩展名的 Windows PDF 文件,因此我倾向于使用-iname
...-print
find 选项(而不是-print0
),因此它将是基于行的(每行一个文件名),然后必须省略-0
(NUL 分隔符)并行命令。--ignore-case
将使搜索不区分大小写。作为使用整个命令行时的一般建议,parallel --dry-run 将打印将执行哪些命令。
Thanks for all the good ideas here!
I tried the xargs method, but as pointed out here, xargs will make it impossible (or very hard) to include printing the actual file name...
So I tried the whole thing with GNU parallel.
--context=5
also 5 lines above and below as well for context.-q
pdftotext won't print any error messages or warnings (quiet).[]
as labels instead of braces{}
. If you wanted braces--label='{'{}'}'
will make that happen. Note that{}
is replaced by the actual filename by GNU parallel, e.g.'Example portable document file name with spaces.pdf'
({}
is already using single quotes'
).--label={}
only the filename will be printed, which may be the favored way of displaying the filename.--color=always
with grep.--ignore-case
to the grep command for a case-insensitive keyword search.If all PDF files should be processed recursively, including all sub-directories in the current directory (
.
), this can be accomplished through find:-iname '*.pdf'
acts case-insensitive. With-name '*.pdf'
only lower-case .pdf files will be included (the normal case). Since I sometimes also encountered Windows PDF-files with an upper-case .PDF file extension, I tend to prefer-iname
...-print
find option (instead of-print0
), so it will be line-based (one file name per line), then-0
(NUL delimiter) must be omitted from the parallel command.--ignore-case
in the grep command will make the search case-insensitive.As a general recommendation when playing with the whole command line,
parallel --dry-run
will print which commands would be executed.使用 pdfgrep:
在此命令中,我在文件夹 < 中搜索单词
FWCOSP
代码>日期模型/。正如您在输出中看到的,您可以使用带有行号的文件名:
我使用的选项是:
Use pdfgrep:
In this command I'm searching for the word
FWCOSP
inside the folderDatenModel/
.As you can see in the output you can have the file name wit the line numbers:
The options I'm using are: