cat命令不显示文本行
我试图从终端读取一个odt文件。 当我输入cat myfile.odt时,它会向我显示这样的图像
Libreoffice格式的文本位于二进制文件的压缩部分中,因此cat
不起作用。 有一个选项: lowriter --convert-to example.txt
将重新打包它,如果你想要的话,还有一个–print选项。 man lowriter
是翔实的。
为什么它不能按预期工作
cat
在文本文件上工作 。 odt文件在技术上(并且非常简化)包含一些xml文件的ziped文件夹。
因此,’ 猫 ‘不能用于此目的。 它仅适用于纯文本。
你可以做什么呢
你当然可以提取它并解析相应的xml文件,但我想这对你的目的来说太过分了。
您正在尝试的替代方案是:
odt2txt --stdout file.odt
这将在txt文件中提供与cat相同的function,但根据文件的大小需要更多时间。 你需要安装unoconv
sudo apt install unoconv
odt文件是一个zip包,其中包含文档的格式和其他function。
我想看看你需要解压缩的odt文件的内容。 文档中包含的实际单词位于content.xml
文件中。
Micosoft word文档(* .docx)是相同类型的包。 word文档的文本位于名为document.xml
的压缩目录的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