LoginSignup
2
3

More than 3 years have passed since last update.

MS-Word文書からテキストをささっと抜き出すシェルスクリプト

Last updated at Posted at 2019-05-26

.docx文書や.odt文書のテキスト部分はZIPアーカイブ内のword/document.xmlやcontent.xmlに他ならない。それから不要な書式タグを取り、段落末タグを改行に置換すればプレーンテキストとなる。
.doc文書はしかたがないのでantiwordなど適当なものを呼び出している。
ただし、書式タグをことごとく消すので、ルビを親文字列の後ろに括弧も付けず書き出すなど、いろいろ不具合もあるのでご注意を。残したいタグは置換を工夫してください。

#!/bin/sh
#
# catdoc - .doc文書、.docx文書のテキストを標準出力へ
#

dext=${1##*.}

if [ $dext = docx ] ; then
        unzip -j "$1" word/document.xml > /dev/null
        sed     -e 's/<\/w:p>/\n/g' \
                -e 's/<w:tab\/>/        /g' \
                -e 's/<[^<]\+>//g' \
                -e 's/&lt;/</g' \
                -e 's/&gt;/>/g' \
                -e 's/&amp;/\&/g' \
                document.xml \
                | tr -d \\r
        rm document.xml
elif [ $dext = odt ] ; then
        unzip -j "$1" content.xml > /dev/null
        sed     -e 's/<\/w:p>/\n/g' \
                -e 's/<w:tab\/>/        /g' \
                -e 's/<[^<]\+>//g' \
                -e 's/&lt;/</g' \
                -e 's/&gt;/>/g' \
                -e 's/&amp;/\&/g' \
                content.xml
#               | tr -d \\r
        rm content.xml
elif [ $dext = doc ] ; then
        antiword -w0 "$1"
else
        echo "わーどちゃうし。"
fi
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3