如何在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命令。

命令

find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}"
--color "func main()"' \;

輸出

./main.go:func main() {

更新於:2021年7月30日

瀏覽量:1K+

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告