3

I want to get all .odt files in some folder recursively, extract their text-content and create .txt files from them (named accordingly, so A.odt -> A.txt)

Problem is, I am no good with shell apart from a few tricks.

grep for this is easy: grep -r -i --include \*.odt .

manpage of odt2txt says, I need to specify --output=FILE

So for one file it would be odt2txt A.txt --output=A.txt

This works like a charm. But how to combine those two?

I face two problems here, normally I would chain my commands (again, shell noob) with pipes, like so

grep -r -i --include \*.odt . | odt2txt $INPUT_FROM_GREP --output=$MISSING_NAME

But as you can see, odt2txt wants the file name as first argument, and how to get the name, without the extension to be used by odt2txt?

I feel like I am not on the right track.

4 Answers 4

3

grep is used to find matching lines in files, but all you seem to want to do is find files whose names match a certain pattern. For that, one would use find. Also, I presume that odt2txt wants A.odt as first argument, not A.txt.

I would use find to find the files, then use its -exec option to execute odt2txt. I'd use basename to strip of the .odt extension, and then I add .txt. So, something like this:

find . -name '*.odt' -exec odt2txt {} --output=`basename {} .odt`.txt ";"

Note that after an -exec, {} denotes the filename, and the end of the command to execute is signalled by ";".

7
  • What about the path in the output filename? basename only delivers the file name without path.
    – ideaboxer
    Sep 2, 2017 at 18:56
  • also minor thing is that files would be called .odt.txt afterwards, not just .txt (but that doesn't really matter at all)
    – user4063815
    Sep 2, 2017 at 18:58
  • Also it's not recursive, but that is easily added :)
    – user4063815
    Sep 2, 2017 at 19:03
  • 1
    find is recursive.
    – ideaboxer
    Sep 2, 2017 at 19:04
  • My bad... it was sooo fast though, oO converting quite a few hundreds of (bigger) texts
    – user4063815
    Sep 2, 2017 at 19:09
2

Using find and while:

find . -name "*.odt" | while read f
do
  odt2txt "$f" --output="`dirname "$f"`/`basename -s ".odt" "$f"`.txt"
done

Oneliner:

find . -name "*.odt" | while read f; do odt2txt "$f" --output="`dirname "$f"`/`basename -s ".odt" "$f"`.txt"; done
1
#/usr/bin/bash
for i in *.odt ; do ( if odt2txt $i| grep -i $1 ;  then echo $i; fi; ) ;done
apt install odt2txt 

and Leave grep alone!

0

Below would do :

function odt2txtfun
{
outfilename=${1##*/}
outfilename=${outfilename%.odt}.txt
odt2txt "$1" --output="/path/to/output/folder/${outfilename}"
}
export -f odt2txtfun
find /folder/that/contains/odt/files -name *.odt" -exec bash -c 'odt2txtfun "$1"' _ {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.