Mar*_*ams 23
Libreoffice 格式在二进制文件的压缩部分中包含文本,因此cat不起作用。有一个选项:lowriter --convert-to example.txt它将重新打包它,如果您想要,还有一个 --print 选项。man lowriter是翔实的。
Bru*_*uni 14
为什么它不像你预期的那样工作
cat适用于文本文件。一个ODT文件在技术上是(非常简化的)一个ziped包含一些文件夹中的XML文件。
因此,“ cat ”不能用于此目的。它仅适用于纯文本。
你可以做什么
您当然可以提取它并解析相应的 xml 文件,但我想这对您的目的来说太过分了。
您正在尝试的另一种选择是:
odt2txt --stdout file.odt
Run Code Online (Sandbox Code Playgroud)
这将在 txt 文件上提供与 cat 相同的内容,但会花费更多时间,具体取决于文件的大小。你需要安装unoconv
sudo apt install unoconv
Run Code Online (Sandbox Code Playgroud)
odt 文件是一个 zip 包,其中包含文档的格式设置和其他功能。
我想查看必须解压缩的 odt 文件的内容。文档中包含的实际单词在content.xml文件中。
Micosoft word 文档 (*.docx) 是同类型的包。Word 文档的文本位于名为 .zip 的压缩子目录的文件中document.xml。
我编写了一个脚本来对我的文档执行文本搜索。该脚本将采用文件的两个参数(文件名和要查找的文本),将文件提取到临时文件夹,grep xml 文件的内容,然后显示与搜索文本匹配的文件名。
用于搜索目录及其子目录中所有 odt 文件的示例脚本:
#!/bin/bash
directory="$1"
string="$2"
tempdir="/tmp/searchdir"
echo "Searching directory [$directory] for [$string]"
echo "---------------------------------------------"
if [ $# -ne 2 ]; then
echo "Parameter error... Usage: [Directory to Search] [String to search]"
echo "Note: Use quotes if spaces are included in directory or search string."
echo "Exiting..."
exit 1
fi
mkdir $tempdir
while IFS= read -r -d '' i;
do
# echo Processing: $i
unzip -o "$i" -d $tempdir content.xml > /dev/null 2>&1
found=$(egrep -i "$string" $tempdir/content.xml)
if [[ "$found" ]]; then
echo "Found in [$i]"
fi
[[ -f /tmp/content.xml ]] && rm /tmp/content.xml # remove the temporary file if exist
done < <(find $directory -name \*odt -print0)
rm -r $tempdir
Run Code Online (Sandbox Code Playgroud)