-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexshell2csv.sh
More file actions
141 lines (127 loc) · 2.91 KB
/
Copy pathexshell2csv.sh
File metadata and controls
141 lines (127 loc) · 2.91 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/sh
####################### exshell2csv version 0.2 ########################
# exshell2csv: Small script to convert Excel to CSV, written in shell script only. No additional packages are required.
# Dependencies: Bourne Shell, sed, awk, and unzip.
# Usage: exshell2csv -h
# Reporitory: https://github.com/minamotorin/exshell2csv
# License: GNU General Public License Version 3 (https://www.gnu.org/licenses/gpl-3.0.html).
########################################################################
if [ "$1" = '-h' ]
then
cat<<EOF
exshell2csv Version 0.2
Usage: `basename $0` [-h] [XLSX] [SHEET ID]
-h : Show this help
[XLSX] : Show list of SHEET IDS of XLSX file
[XLSX] [SHEET ID]: Convert XLSX file’s SHEET ID to CSV (output to STDOUT)
EOF
exit
fi
if ! [ -e "$1" ]
then
echo 'Error: file '"$1"' doesn’t exist' >&2
exit 1
fi
if [ "$2" = '' ]
then
unzip -p "$1" xl/workbook.xml |
sed '
$!N
H
$!D
x
s/\n//g
s/.*<sheets>//
s/<\/sheets>.*//
s/<sheet/\
&/g
' |
sed 's/.* name="\(.*\)" sheetId="\([^"]*\)".*/\2: \1/; /^$/d'
exit $?
fi
(
unzip -p "$1" xl/sharedStrings.xml |
awk '{gsub("\\r", ""); print}' |
sed '
1{
s/^<?xml [^>]*>//
/^$/d
:loop
/^[^<]/ s/.//
t loop
}
' |
sed '
1{/^<?xml/d;}
s/<si>/\
&/g
s/<si [^>]*>/\
<si>/g
' |
sed '
1d
:loop
/<\/si>/!{
N
bloop
}
:si
/^<si>/! s/.//
t si
s/^<si>//
s/\\/&&/g
s/\n/\\n/g
s/<\/si>.*//
' |
sed '
:topen
/^<t>/!{ /^<t /!{
s/^<[^>]*>//
t topen
}; }
s/^<t>//
s/^<t [^>]*>//
H
x
s/<\/t>.*//
s/\n//
x
:tclose
/^<\/t>/!{
s/.//
t tclose
}
s/^<\/t>//
/<\/t>/ b topen
s/.*//
x
' |
sed 's/^/l /'
echo
unzip -p "$1" xl/worksheets/sheet"$2".xml |
sed '
s/<c [^>]*>/\
&\
/g
s/<\/c>/\
&\
/g
' |
sed -n '/<c .*[^/]>/, /<\/c>/p' |
sed '
/^<c /{
/t="s"/ s/.* r="\([^"]*\)" .*/\1 s/
/^<c/ s/.* r="\([^"]*\)" .*s="\([0-9]*\)".*/\1 \2/
/^<c/ s/.* r="\([^"]*\)">/\1 v/
:loop
N
/<\/c>$/! bloop
s/\n/ /g
}
s/<\/\{0,1\}[vc]>//g
s/<f\( [^>]*\)\{0,1\}>.*<\/f>//
s/<f [^>]*\/>//
s/^[A-Z]*/& /
'
) |
cat