Skip to content

Instantly share code, notes, and snippets.

@texadactyl
Last active June 11, 2018 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save texadactyl/16d234fe57aaa8d9445481df6097898d to your computer and use it in GitHub Desktop.
Save texadactyl/16d234fe57aaa8d9445481df6097898d to your computer and use it in GitHub Desktop.
#!/bin/bash
#=============================================================
# Clean up a text file for importing into a LibreOffice Word Processor
# document (file extension = odt).
# Input: $1 (dirty text file)
# Output: $2 (LibreOffice Writer ODT file)
#
# Version 2 on 2018-06-15 by richard.elkins@gmail.com
# Amend and/or distribute as you wish.
#
# Strategy for each file:
# 1. Change all control characters (0x00 through 0x0f) into space characters.
# 2. Delete all leading and trailing whitespace (blanks, tabs).
# 3. For file conversion: libreoffice --convert-to odt:writer8 {text file}
#=============================================================
#--- Definitions
MYNAME=`basename $0`
TEMP_PREFIX=${MYNAME}_$$
TEMP_TXT_FILE=./$TEMP_PREFIX.txt
TEMP_ODT_FILE=./$TEMP_PREFIX.odt
# Process one file (callable procedure)
proc_one_file()
{
IN=$1
OUT=$2
ORIGINAL=$IN.original
#--- Check readability of input file
if [ ! -r $IN ]; then
echo '*** '$MYNAME': File '$IN' does not exist or is not readable'
exit 86
fi
#--- Check writability of input file
if [ ! -w $IN ]; then
echo '*** '$MYNAME': File '$IN' is not writable'
exit 86
fi
#--- Temp file = input file scrubbed of control characters
tr -s [:cntrl:] ' ' < $IN > $TEMP_TXT_FILE
RC=$?
if [ $RC -ne 0 ]; then
echo '*** '$MYNAME': tr using '$IN' failed'
exit 86
fi
# Convert $IN to ODT format
libreoffice --headless --convert-to odt:writer8 $TEMP_TXT_FILE
RC=$?
if [ $RC -ne 0 ]; then
echo '*** '$MYNAME': libreoffice conversion failed of '$IN
exit 86
fi
sleep 1
# Move ODT-version of $IN to $OUT
cp $TEMP_ODT_FILE $OUT
RC=$?
if [ $RC -ne 0 ]; then
echo '*** '$MYNAME': mv '$IN' '$OUT' failed'
exit 86
fi
# Delete files no longer needed
rm $IN
rm $TEMP_TXT_FILE
rm $TEMP_ODT_FILE
# SUCCESS
echo $MYNAME'('$IN'): done'
}
#--- Begin main script
#--- Check command line
ARGC=$#
if [ $ARGC -ne 2 ]; then
echo
echo "Usage: "$MYNAME" {Input Text File} {Output ODT File}"
echo
exit 1
fi
# Make sure that the temporary file is writable
touch $TEMP_TXT_FILE
RC=$?
if [ $RC -ne 0 ]; then
echo '*** '$MYNAME': touch '$TEMP_TXT_FILE' failed'
exit 86
fi
# Convert specified text file ($1) file to an ODT file ($2)
proc_one_file $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment