10

This question already has an answer here:

i am trying to read an odt file from terminal. When i type cat myfile.odt it shows me an image like this

enter image description here

marked as duplicate by andrew.46, waltinator, Byte Commander, Kevin Bowen, heemayl Sep 23 '16 at 20:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 10
    Well, the .odt file is a binary, so when you cat it, you shouldn't expect you can understand the output. :) – edwinksl Sep 23 '16 at 8:36
  • 7
    Some insights here: askubuntu.com/q/546765/57576 – andrew.46 Sep 23 '16 at 10:53
22

Libreoffice format has the text within a compressed section of a binary file, so cat doesn't work. There is an option: lowriter --convert-to example.txt which will repackage it, & there is a --print option if that's what you wanted. man lowriter is informative.

  • this answer should be on top : it doesn't try to fix the method the OP tried, but to fix the problem the OP had (ie : displaying a .odf content from the terminal). +1 (see: meta.stackexchange.com/a/66378/235138) – Olivier Dulac Sep 23 '16 at 11:06
  • oops, I should have given the link to the 2nd (and best) answer (explaining better the concept of XYProblem) : meta.stackexchange.com/questions/66377/what-is-the-xy-problem/… . But I'm glad that your answer is now the top answer! – Olivier Dulac Sep 23 '16 at 14:10
  • 3
    It is wrong that cat does not work. cat works as intended. – dbanet Sep 23 '16 at 18:46
  • cat works in the sense that it functions correctly. It doesn't work in this context, ie doing what the OP intended the outcome to be. It is to be hoped that we can avoid superfluous pedantry when the context is clear, because I fear that merely muddies the waters of usefulness :) cat is of course an immensely useful tool - in the correct context. This is an important point - it isn't a tool requiring complex options, and doesn't attempt to please everyone - the route to bloatware! – Mark Williams Sep 24 '16 at 9:14
14

Why it does not work as you expected

cat works on text files. An odt file is technically (and very simplified) a ziped folder containing some xml files.

As such 'cat' can not be used for this purpose. It works only on plain text.

What you can do instead

You could of course extract it and parse the respective xml files, but I guess this is overkill for your purposes.

An alternative for what you are trying is:

odt2txt --stdout file.odt

this will provide the same as cat on a txt file, but will take more time depending on the size of the file. you will need to have unoconv installed

sudo apt install unoconv
6

The odt file is a zip package that include formatting and other features for the document.

I you want to see the content of an odt file you would have to unzip. The actual words contained in the document is in the content.xml file.

Micosoft word documents (*.docx) is the same type of package. The text of a word documents is in a file of a zipped sudirectory named document.xml.

I wrote a script to perform text search on my documents. The script would take two argument for the file (filename and text to find), extract the file to a temp folder, grep the contents of the xml file then display the filename that matches the text searched.


Sample Script to search all odt files in a directory and it's subdirectories:

#!/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
  • 1
    Nice that you wrote a script, but without including it into your answer or providing a link, it is not very useful... – Byte Commander Sep 23 '16 at 18:31
  • @ByteCommander Thanks for the nudge. Script added to the answer. – L. D. James Sep 23 '16 at 20:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.