Skip to content

Instantly share code, notes, and snippets.

@axon-obriend
Last active March 20, 2018 18:39
Show Gist options
  • Select an option

  • Save axon-obriend/408e14b54e525a7b925a9d2e0c5322ea to your computer and use it in GitHub Desktop.

Select an option

Save axon-obriend/408e14b54e525a7b925a9d2e0c5322ea to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script uses LibreOffice Calc in headless mode to convert a tab-separated values file
# to a comma-separated values file. Also replaces MySQL NULLs with empty string.
if [[ $# -ne 1 ]]; then
echo "usage: `basename $0` filename"
echo "where 'filename' is a file of tab-separated values. 'filename' will be converted"
echo "to comma-separated values and renamed 'filename.bak'."
exit 1
fi
LIBRE_PATH=`which scalc`
[ -z "$LIBRE_PATH" -a -x "/Applications/LibreOffice.app/Contents/MacOS/soffice" ] && LIBRE_PATH="/Applications/LibreOffice.app/Contents/MacOS/soffice"
if [ -f $1 ]; then
$LIBRE_PATH --headless --infilter="Text - txt - csv (StarCalc):9,34,0,1," \
--convert-to csv:"Text - txt - csv (StarCalc):44,34,0,1," \
--outdir /tmp $1
mv $1 $1.bak
mv /tmp/$1 $1
sed -i -e 's/,NULL/,/g' $1
else
echo "File $1 not found"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment