如何在Linux系统上搜索多个PDF文件的内容?
Linux中的pdfgrep命令用于在PDF文件(单个或多个)中搜索特定字符模式。它是Linux中最常用的实用程序命令之一,用于显示包含我们要搜索的模式的行。
通常,我们在文件中搜索的模式被称为正则表达式。
安装Pdf grep
适用于Ubuntu/Fedora
sudo apt-get update -y
sudo apt-get install -y pdfgrep
适用于CentOS
yum install pdfgrep
语法
pdfgrep [options...] pattern [files]
虽然有很多不同的选项可用,但一些最常用的选项是:
-c : counts the number of matches per input file. -h : suppresses the prefixing of file name on output. -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 : recursively search all files -R : same as -r, but it also follows all symlinks.
现在,让我们考虑一个案例,我们想在一个特定目录(例如dir1)中的所有pdf文件中查找特定模式。
语法
pdfgrep -HiR "word" *
在上面的命令中,用…替换“word”占位符
为此,我们使用以下命令:
pdfgrep -HiR "func main()" *
上面的命令将尝试在特定目录以及子目录中的所有文件中查找字符串“func main()”。
输出
main.go:120:func main() {}
如果我们只想在一个目录中查找特定模式,而不是在子目录中查找,则需要使用以下命令:
pdfgrep -i "func main()" *
在上面的命令中,我们使用了-s标志,这将帮助我们避免在运行命令的目录中存在的每个子目录中出现警告。
输出
main.go:120:func main() {}
另一个我们可以使用的命令是find命令。
Learn Linux/Unix in-depth with real-world projects through our Linux/Unix certification course. Enroll and become a certified expert to boost your career.
命令
find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "func main()"' \;
输出
./main.go:func main() {
广告