如何在目录/子目录中搜索PDF文件的内容?我在找一些命令行工具。grep似乎不能搜索PDF文件。
当前回答
我写了这个破坏性的小脚本。祝你玩得开心。
function pdfsearch()
{
find . -iname '*.pdf' | while read filename
do
#echo -e "\033[34;1m// === PDF Document:\033[33;1m $filename\033[0m"
pdftotext -q -enc ASCII7 "$filename" "$filename."; grep -s -H --color=always -i $1 "$filename."
# remove it! rm -f "$filename."
done
}
其他回答
使用pdfgrep:
pdfgrep -HinR 'FWCOSP' DatenModel/
在这个命令中,我在DatenModel/文件夹中搜索单词FWCOSP。
正如你在输出中看到的,你可以有文件名和行号:
我使用的选项是:
-i : Ignores, case for matching
-H : print the file name for each match
-n : prefix each match with the number of the page where it is found
-R : same as -r, but it also follows all symlinks.
Recoll是一个很棒的Unix/Linux全文GUI搜索应用程序,支持几十种不同的格式,包括PDF。它甚至可以将查询的确切页码和搜索词传递给文档查看器,从而允许您直接从它的GUI跳转到结果。
Recoll还提供了一个可行的命令行界面和一个web浏览器界面。
我写了这个破坏性的小脚本。祝你玩得开心。
function pdfsearch()
{
find . -iname '*.pdf' | while read filename
do
#echo -e "\033[34;1m// === PDF Document:\033[33;1m $filename\033[0m"
pdftotext -q -enc ASCII7 "$filename" "$filename."; grep -s -H --color=always -i $1 "$filename."
# remove it! rm -f "$filename."
done
}
试着在一个简单的脚本中使用'acroread',就像上面那样
你的发行版应该提供一个名为pdftotext的实用程序:
find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' \;
如果要将pdftotext输出到标准输出,而不是输出到文件,则必须使用“-”。 ——with-filename和——label=选项将把文件名放在grep的输出中。 可选的——color标志很好,它告诉grep在终端上使用颜色输出。
(在Ubuntu中,pdftotext是由xpdf-utils或poppler-utils包提供的。)
如果您想使用GNU grep中pdfgrep不支持的特性,这种使用pdftotext和grep的方法比pdfgrep更有优势。注意:pdfgrep - 1.3。x支持-C选项打印上下文行。