命令行将目录(和子目录)中的所有 .docx 转换为文本文件并写入新文件

jej*_*uba 3 grep doc unzip

我想从命令行将目录(和子目录)中的所有 .docx 文件转换为文本文件(这样我可以在这些文件上使用 grep after )。我找到了这个

unzip -p tutu.docx word/document.xml | sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g'
Run Code Online (Sandbox Code Playgroud)

这里效果很好,但它在终端中发送文件。我想将新的文本文件(例如 .txt)写入与 .docx 文件相同的目录中。我想要一个脚本来递归地执行此操作。

我有这个,使用反词,可以完成我想要的 .doc 文件,但它不适用于 .docx 文件。

find . -name '*.doc' | while read i; do antiword -i 1 "${i}" >"${i/doc/txt}"; done
Run Code Online (Sandbox Code Playgroud)

我尝试将两者混合,但没有成功...能够同时执行这两项操作的命令行将不胜感激!

谢谢

Dav*_*hel 5

您可以使用pandoc来转换 docx 文件。它不支持.doc文件,因此您需要 pandoc 和 antiword。

重用你的while循环:

find . -name '*.docx' | while read i; do pandoc --from docx --to plain "${i}" >"${i/docx/txt}"; done
Run Code Online (Sandbox Code Playgroud)