-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv.sh
More file actions
executable file
·53 lines (48 loc) · 814 Bytes
/
csv.sh
File metadata and controls
executable file
·53 lines (48 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
parse_csv() {
line="${1}"
length="${#line}"
position=0
if [ $length = 0 ]; then
return
fi
token=""
quoted=0
while [ $position -lt $length ]; do
current="${line:$position:1}"
case "${current}" in
\")
if [ $quoted = 0 ]; then
quoted=1
else
quoted=0
fi
prev_pos=$(( position - 1 ))
prev="${line:$prev_pos:1}"
if [ "$prev" = \" ]; then
token="${token}${current}"
fi
;;
,)
if [ $quoted = 0 ]; then
break
else
token="${token}${current}"
fi
;;
*)
token="${token}${current}"
;;
esac
(( ++position ))
done
(( ++position ))
echo "${token}"
parse_csv "${line:$position}"
}
if [ "$0" = "$BASH_SOURCE" ]; then
while read -r line ; do
array=( $(parse_csv "${line}") )
declare -p array
done
fi