From f80dde248765ac86705f4b0a3e4dac5157b547ae Mon Sep 17 00:00:00 2001 From: vv01f Date: Sun, 17 Nov 2019 00:23:10 +0100 Subject: [PATCH] convert to target csv format --- .gitignore | 1 + dhl.sh | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index d6b3082..ccb40ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.csv baerer +*.bak diff --git a/dhl.sh b/dhl.sh index 6ea2c36..620a737 100755 --- a/dhl.sh +++ b/dhl.sh @@ -1,13 +1,24 @@ #!/usr/bin/env sh +dependencies="sed test cp libreoffice" +assert_tools () { + while test $# -gt 0; do + which $1 >/dev/null 2>/dev/null || { + echo "tool missing: "$1 + exit 2 + } + shift + done +} +assert_tools ${dependencies} # target filename -tn="dhl-ds-versendung.csv" +tn="dhl-cp1252.csv" # todo: download file (its UTF-8 encoded) -fn="dhl-ds-versendung-3.csv" +fn="dhl.csv" # backup -cp -f "$fn" "dhl.csv" +cp -f "${fn}" "${fn}.bak" # replace special spaces and hyphens sed -i -e 's/[\u00A0\u202F[:space:]]+/ /g' -e 's/[\u2010\u2011\u2012\u2013\u002D]+/-/g' -e 's/[\u00AD\uFEFF]+//g' "$fn" @@ -77,3 +88,19 @@ testiso3 () { testcmp "" "iso3countrycode" "Simbabwe" # should fail } #~ testiso3 + +convcsv () { + if test $# -eq 0 ; then echo "argument missing."; exit 1; fi + if test -e $1 ; then echo "file already exists."; exit 2; fi +# charset windows codepage 1252 +# doc for headless conversion options https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options + if="${1}" + of=$(echo "${if}"|rev|cut -d. -f2-|rev)".ods" + libreoffice --headless --convert-to ods --infilter=CSV:59,34,76,1 "${if}" && { + mv "${if}" "${if}.bak" + libreoffice --headless --infilter=CSV:59,34,76,1 --convert-to csv:"Text - txt - csv (StarCalc)":"44,34,1,0,1,,0" "${tf}" && { + rm "${tf}" "${if}.bak" + } + } +} +convcsv "$fn"