gpt4 book ai didi

linux - 如何将 xargs 与 pdftotext 转换器耦合以在多个 pdf 文件中搜索

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:54 25 4
gpt4 key购买 nike

我正在制作一个脚本,该脚本应该在目录中的所有 pdf 文件中进行搜索。我找到了一个名为“pdftotext”的转换文件,它使我能够在 pef 文件上使用 grep,但我只能用一个文件运行它。当我想对目录中存在的所有文件运行它时,它会失败。有什么建议 ?

这有效:对于单个文件

pdftotext my_file.pdf - | grep 'hot'

这失败了:用于搜索 pdf 文件并转换为文本和 greping

SHELL PROMPT>find ~/.personal/tips -type f -iname "*" | grep -i "*.pdf" | xargs pdftotext |grep admin
pdftotext version 3.00
Copyright 1996-2004 Glyph & Cog, LLC
Usage: pdftotext [options] <PDF-file> [<text-file>]
-f <int> : first page to convert
-l <int> : last page to convert
-layout : maintain original physical layout
-raw : keep strings in content stream order
-htmlmeta : generate a simple HTML file, including the meta information
-enc <string> : output text encoding name
-eol <string> : output end-of-line convention (unix, dos, or mac)
-nopgbrk : don't insert page breaks between pages
-opw <string> : owner password (for encrypted files)
-upw <string> : user password (for encrypted files)
-q : don't print any messages or errors
-cfg <string> : configuration file to use in place of .xpdfrc
-v : print copyright and version info
-h : print usage information
-help : print usage information
--help : print usage information
-? : print usage information
SHELL PROMPT 139>

最佳答案

xargs 是这项工作的错误工具:find 内置了您需要的一切。

find ~/.personal/tips \
-type f \
-iname "*.pdf" \
-exec pdftotext '{}' - ';' \
| grep hot

就是说,如果您确实出于某种原因想要使用xargs,正确的用法应该类似于...

find ~/.personal/tips \
-type f \
-iname "*.pdf" \
-print0 \
| xargs -0 -J % -n 1 pdftotext % - \
| grep hot

注意:

  • find 命令使用 -print0 对其输出进行 NUL 分隔
  • xargs 命令使用 -0 对其输入进行 NUL 分隔(这也关闭了一些行为,这些行为会导致不正确地处理名称中包含空格的文件名,文字引号字符等)。
  • xargs 命令使用-n 1 为每个文件调用一次pdftotext
  • xargs 命令使用 -J % 指定替换应该发生的位置的标记,并在 pdftotext 命令中使用该 %适本地行。

关于linux - 如何将 xargs 与 pdftotext 转换器耦合以在多个 pdf 文件中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29232229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com