convert \n to \r\n for filecontent

This commit is contained in:
vv01f 2019-11-17 03:26:28 +01:00
parent ed1772e76d
commit 45694306b6
Signed by untrusted user who does not match committer: vv01f
GPG Key ID: 02625A16AC1D1FF6
1 changed files with 54 additions and 12 deletions

66
dhl.sh
View File

@ -1,5 +1,10 @@
#!/usr/bin/env sh
dependencies="sed test cp libreoffice expr cut"
# target filename
tn1="dhl-cp1252-n.csv"
tn2="dhl-cp1252-rn.csv"
assert_tools () {
while test $# -gt 0; do
which $1 >/dev/null 2>/dev/null || {
@ -17,11 +22,28 @@ testcmp () {
echo "arguments missing."; exit 1
fi
# whitelisted functions
case $2 in
case $2 in
# 2 arg functions where file exists
convnl)
expect="$1"
fn="$2"
shift 2
if test -e "$expect" ; then
echo "not testing as result $expect already exists."
else
result=$("$fn" "$1" "$2")
if test -e $expect ; then
echo "passed $fn for: $1 $2."
else
echo "failed $fn for: $1 $2."
fi
fi
;;
# 2 arg functions
chklength|stringcrop)
expect="$1"; shift
fn="$1"; shift
expect="$1"
fn="$2"
shift 2
while test "$#" -ge "2"; do
result=$("$fn" "$1" "$2")
if test "$result" = "$expect" ; then
@ -34,8 +56,9 @@ testcmp () {
;;
# 1 arg functions
iso3countrycode)
expect="$1"; shift
fn="$1"; shift
expect="$1"
fn="$2"
shift 2
for a in "$@"; do
result=$("$fn" "$a")
if test "$result" = "$expect" ; then
@ -61,7 +84,6 @@ testchklength () {
testcmp "true" "chklength" "3" "123"
testcmp "3" "chklength" "2" "1-2"
}
testchklength
stringcrop () {
echo "$1" | cut -c1-"$2"
@ -70,7 +92,6 @@ stringcrop () {
teststringcrop () {
testcmp "string" "stringcrop" "string too long" "6"
}
teststringcrop
# convert country to countrycode as in ISO3
iso3countrycode () {
@ -103,17 +124,38 @@ testiso3 () {
testcmp "CHE" "iso3countrycode" "Schweiz"
testcmp "" "iso3countrycode" "Simbabwe" # should fail
}
testiso3
exit 0;
# target filename
tn="dhl-cp1252.csv"
# replace newline 0a with 0d 0a for dos version
# test with: echo | sed 's/$'"/`echo \\\r`/" | hexdump
convnl () {
if="$1"
of="$2"
shift 2
expect="0a0d" # hexdump does reverse the byte order
result=$(echo | sed 's/$'"/`echo \\\r`/" | hexdump |head -1|cut -d" " -f2)
if test $expect = $result ; then
sed -e 's/$'"/`echo \\\r`/" "$if" > "$of"
fi
}
testconvnl () {
testcmp "$tn2" "convnl" "$tn1" "$tn2" && rm "$tn2"
}
runtests () {
testchklength
teststringcrop
testiso3
testconvnl
}
runtests
# todo: download file (its UTF-8 encoded)
fn="dhl.csv"
# backup
cp -f "${fn}" "${fn}.bak"
cp -f "$fn" "${fn}.bak"
exit 0;
# 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"