用于搜索 docx 文件的命令行工具

Gob*_*0st 11 grep cygwin search microsoft-word

是否有用于文本搜索 docx 文件的命令行工具?我试过grep,但它不适用于 docx,即使它适用于 txt 和 xml 文件。我可以先将 docx 转换为 txt,但我更喜欢直接对 docx 文件进行操作的工具。我需要该工具在 Cygwin 下工作。

OP 编辑​​:后来我发现实现 grep 的最简单方法实际上是将这些 docx 转换为 txt 然后 grep 覆盖它们。

Cam*_*sei 7

我的grep解决方案作为一个函数,您可以粘贴到您的.bashrc

docx_search(){ local arg wordfile terms=() root=${root:-/}; for arg; do terms+=(-e "$arg"); done; find 2>/dev/null "${root%/}/" -iname '*.docx' -exec bash -c "$(declare -p terms)"'; for arg; do unzip -p "$arg" 2>/dev/null | grep --quiet --ignore-case --fixed-strings "${terms[@]}" && printf %s\\n "$arg"; done' _ {} +; }
Run Code Online (Sandbox Code Playgroud)

它将查找其参数的任何出现(不区分大小写)并打印匹配的 docx 文件位置。


例子:

$ docx_search 'my example sentence'
/cygdrive/d/example sentences.docx
/cygdrive/c/Users/my user/Documents/example sentences.docx
$ root='/cygdrive/c/Users/my user/' docx_search 'seldom' 'full sentence'
/cygdrive/c/Users/my user/Documents/example sentences.docx
$ 
Run Code Online (Sandbox Code Playgroud)

可读版本:

docx_search(){
  local arg wordfile terms=() root=${root:-/}
  # this 'root' assignment allows you to search in a specific location like /cygdrive/c/ instead of everywhere on the machine
  for arg; do terms+=(-e "$arg"); done
  # We inject the terms to search inside the string with declare -p`
  find 2>/dev/null "${root%/}/" -iname '*.docx' -exec \
    bash -c "$(declare -p terms)"';
      for arg; do
        unzip -p "$arg" 2>/dev/null |
          grep --quiet --ignore-case --fixed-strings "${terms[@]}" &&
          printf %s\\n "$arg"
      done' _ {} +
}
Run Code Online (Sandbox Code Playgroud)


Nil*_*ils 2

DOCx 是压缩的,它不是文本格式。所以你首先需要一个转换器。之后,您可以find在转换后的文件上使用该命令。