Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/readcsv.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
tmp1=$tmp/tmp1.csv
35
tmp2=$tmp/tmp2.csv
36
cat > $tmp1 <<- \EOF
37
CAT,"CVE CCODE","NECA OCN",ST,LATA,AP,"New InterState
38
Orig","New Inter""""State
39
Term","New IntraState
40
Orig","New IntraState
41
Term"
42
CLEC,XXXX,AAAA,RB,ABC,comp," 0.2 "," 0.4 "," 0.6 "," 0.8 "
43
CLEC,YYYY,QQQQ,SX,123,mmmm," 0.3 "," 0.5 "," 0.7 "," 0.9 "
44
EOF
45
integer count=0 nfields
46
IFS=${2-,}
47
for j in 1 2
48
do typeset -a arr
49
while read -A -S arr
50
do ((nfields=${#arr[@]}))
51
if ((++count==1))
52
then ((nfields==10)) || err_exit 'first record should contain 10 fields'
53
[[ ${arr[7]} == $'New Inter""State\nTerm' ]] || err_exit $'7th field of record 1 should contain New Inter""State\nTerm'
54
fi
55
for ((i=0; i < nfields;i++))
56
do delim=$IFS
57
if ((i == nfields-1))
58
then delim=$'\r\n'
59
fi
60
if ((i==1))
61
then printf "%#q%s" "${arr[i]}" "$delim"
62
else printf "%(csv)q%s" "${arr[i]}" "$delim"
63
fi
64
done
65
done < $tmp1 > $tmp2
66
done
67
diff "$tmp1" "$tmp2" >/dev/null 2>&1 || err_exit "files $tmp1 and $tmp2 differ"
68
69
exit $((Errors<125?Errors:125))
70
71
72