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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
| #!/bin/sh
# email-export-csv.sh
# Version: 0.9b
#
# Leest tekst in vanuit een e-mail exportbestand en maakt een comma
# separated tabel met de velden die in de mailformulieren gebruikt zijn.
#
# Deze tabel is o.a. in te lezen met OpenOffice Calc, Excel, Filemaker, PHPMyAdmin.
#
# Kan zowel overweg met header-style name/value pairs als met pseudo-XML.
#
# INVOER:
# | Van: test@domain.tld
# | Verzonden: vrijdag 21 september 2007 15:12
# | Aan: test@domain.tld
# | Onderwerp: Webformulier
# |
# | <form>
# | f1
# | <name>Naam</name>
# | <br />
# | <part>
# | p1
# | <item>Tekst</item>
# | p2
# | </part>
# | f2
# | </form>
# |
#
# UITVOER:
# | "N","","Van","Verzonden","Aan","Onderwerp","/form","/form/name","/form/part","/form/part/item"
# | "0",""
# | "1","","test@domain.tld","vrijdag 21 september 2007 15:12",
# | "test@domain.tld","Webformulier","f1 <br /> f2","Naam","p1 p2","Tekst"
# | "2","","Van","Verzonden","Aan","Onderwerp","/form","/form/name","/form/part","/form/part/item"
#
# Systeemomgeving:
# - MacOSX of Linux
#
# Auteur: Ben van Setten, Settembre.
# Licentie: Vrij, als in GPL.
# Init
script_name="mailforms-export-csv"
script_file=$0
script_version="v0.9b"
# Switches for verbose run, quiet run
do_verbose=false
do_quiet=false
do_debug=false
do_error=false
do_help=false
# Preset regular expressions
do_record_mark="^Van:|^From:" # The first line of a new e-mail
do_header_mark="^[0-9A-Za-z ]+:" # The header lines (like 'Subject: ')
do_header_name="[0-9A-Za-z]+" # The name from a header line (used for column header)
do_tags_inline="<[^>]+>" # What a typical XML tag looks like ('<blah>')
do_tags_header="[0-9A-Za-z_]+" # The name of an XML tag (used for column header)
do_tags_ending="^</" # What a typical XML ending tag looks like ('</blah>')
do_tags_single="/>$" # What a single XML tag looks like ('<blah/>')
do_text_cleanup="[ \t]+" # Characters you don't want in the export text
# Columnn name delimiter
do_name_delimiter="/" # For nested object hierarchy ('/form/part_01/fields/item_01')
# Files
do_file_in=""
do_file_out=""
do_file_tmp="/tmp/${script_name}-tempfile.tmp"
# Interprete parameters
while getopts vdfhi:o:-: OPT
do
i="-$OPT$OPTARG"
case $i in
--verbose | -v)
do_verbose=true
;;
--debug | -d)
do_debug=true
;;
--quiet | --quietly | -f)
do_quiet=true
;;
--help | -h)
do_help=true
;;
--input=* )
do_file_in=${i#--input=}
;;
-i=* )
do_file_in=${i#-i=}
;;
--output=* )
do_file_out=${i#--output=}
;;
-o=* )
do_file_out=${i#-o=}
;;
-? )
do_error=true
do_help=true
;;
esac
done
shift $((OPTIND-1)); OPTIND=1
# For help
if [[ $do_help == true ]]; then
echo ""
echo "Welcome to ${script_name} ${script_version}"
echo ""
echo "Usage:"
echo "'${script_file} [--help | -h]' to show this help."
echo "'${script_file} [-v | --verbose] [-d | --debug] [-f | --quiet] [inputfile] [outputfile]'"
echo "'${script_file} [-v | --verbose] [-d | --debug] [-f | --quiet] [-i=file | --input=file] [-o=file | --output=file]'"
echo ""
echo "Samples:"
echo "'sh ${script_file} -vd inputfile.txt'"
echo "'cat inputfile.txt | ${script_file} -f > outputfile.csv'"
echo ""
exit 2
fi
# Set input file
if [[ -n "$1" && $do_file_in == "" ]]; then
do_file_in=$1
shift
fi
# Set output file
if [[ -n "$1" && $do_file_out == "" ]]; then
do_file_out=$1
shift
fi
# Test input file
if [[ $do_file_in == "" ]]; then
do_file_in="/dev/stdin"
fi
# Test output file
if [[ $do_file_out == "" ]]; then
do_file_out="/dev/stdout"
fi
# Build the CSV file
if [[ $do_quiet == false ]]; then
echo "Reading mail forms from '${do_file_in}'..."
fi
if [[ $do_verbose == true ]]; then
echo "Building comma separated data table in '${do_file_tmp}'..."
fi
awk -v greprecord="${do_record_mark}" \
-v headermark="${do_header_mark}" \
-v headername="${do_header_name}" \
-v grepobject="${do_tags_inline}" \
-v grepheader="${do_tags_header}" \
-v grepending="${do_tags_ending}" \
-v grepsingle="${do_tags_single}" \
-v grepclean="${do_text_cleanup}" \
-v delimiter="${do_name_delimiter}" \
-v debugging="${do_debug}" \
' \
# \
# Looks through the column header list, \
# returns existing column, or adds one. \
# \
function getcol(f) { \
c = 0; \
j = 0; \
for (i in fields) { \
if (f == fields[i]) { \
c = i; \
if (debugging == "true") { \
print "Found column (col " c "): " f; \
} \
} \
j++; \
} \
if (c == 0) { \
fields[j] = f; \
c = j; \
if (debugging == "true") { \
print "Added column (col " c "): " f; \
} \
} \
return c; \
} \
# \
# Polishes up text for database. \
# \
function polish(v) { \
sub(grepclean, " ", v); \
sub("^[ ]+|[ ]+$", "", v); \
sub("[\"]", "`", v); \
return v; \
} \
# \
# Prints the collected values. \
# \
function export(d) { \
l = "\"" linecount++ "\""; \
j = 0; \
for (i in fields) { \
v = d[j++]; \
sub("[ ]+$", "", v); \
l = l ",\"" v "\""; \
} \
print l; \
} \
# \
# Resets the data stack. \
# \
function cleanupstack() { \
delete values; \
delete traces; \
values[0] = ""; \
traces[0] = 0; \
nestlevel = 0; \
} \
# \
BEGIN { \
linecount = 0; \
fields[0] = ""; \
values[0] = ""; \
traces[0] = 0; \
nestlevel = 0; \
} \
# \
{ \
buffer = $0 \
# \
# Look if we have a match identifying a new record \
# \
if (match(buffer, greprecord)) { \
if (debugging == "true") { \
print "Found beginning of new record."; \
print "Exporting completed record: " linecount; \
} \
export(values); \
cleanupstack(); \
} \
# \
# Look if we have a match identifying a single-line header \
# \
if (match(buffer, headermark)) { \
v = substr(buffer, RSTART, RLENGTH); \
l = length(buffer); \
n = RSTART + RLENGTH; \
m = match(v, headername); \
f = substr(v, RSTART, RLENGTH);; \
v = substr(buffer, n, l - n + 1); \
v = polish(v); \
if (debugging == "true") { \
print "Enter tag: " f; \
} \
c = getcol(f); \
values[c] = values[c] v " "; \
if (debugging == "true") { \
print "Added value (col " c "): " v; \
} \
} \
else { \
# \
# Interprete XML style tags and values \
# \
while ((l = length(buffer)) != 0) { \
# \
# Check for XML style brackets \
# \
m = match(buffer, grepobject); \
if (m == 0) { \
RSTART = length(buffer) + 1; \
RLENGTH = 0; \
} \
n = RSTART + RLENGTH; \
# \
# Read contents before XML tag or line end \
# \
if (nestlevel > 0 && RSTART > 1) { \
v = substr(buffer, 1, RSTART - 1); \
v = polish(v); \
c = traces[nestlevel]; \
values[c] = values[c] v " "; \
if (debugging == "true") { \
print "Added value (col " c "): " v; \
} \
} \
# \
# Interprete XML style tag \
# \
if (m != 0) { \
v = substr(buffer, RSTART, RLENGTH); \
m = match(v, grepending); \
if (m != 0) { \
if (debugging == "true") { \
print "Leave object (level " nestlevel "): " v; \
} \
nestlevel--; \
} \
else { \
m = match(v, grepheader); \
o = substr(v, RSTART, RLENGTH); \
f = fields[traces[nestlevel]] delimiter o; \
m = match(v, grepsingle); \
if (m == 0) { \
nestlevel++; \
if (debugging == "true") { \
print "Enter object (level " nestlevel "): " v; \
} \
traces[nestlevel] = getcol(f); \
} \
else { \
v = polish(v); \
c = traces[nestlevel]; \
values[c] = values[c] v " "; \
if (debugging == "true") { \
print "Added single tag (col " c "): " v; \
} \
} \
} \
} \
# \
# Loop using the remainder of the buffer \
# \
buffer = substr(buffer, n, l - n + 1); \
} \
} \
} \
# \
END { \
# \
# Write out last record and field headers \
# \
if (debugging == "true") { \
print "End of data."; \
print "Exporting completed record: " linecount; \
} \
export(values); \
if (debugging == "true") { \
print "Exporting field headers: "; \
} \
export(fields); \
}' \
"${do_file_in}" \
> "${do_file_tmp}"
# Write out
if [[ $do_quiet == false ]]; then
echo "Writing to '${do_file_out}'..."
fi
tail -n 1 "${do_file_tmp}" | sed 's/^"[0-9]"*/"N"/' > "${do_file_out}"
cat "${do_file_tmp}" >> "${do_file_out}"
# Cleanup tempfiles
if [[ $do_verbose == true ]]; then
echo "Cleaning up tempfile '${do_file_tmp}'..."
fi
rm "${do_file_tmp}"
# Done
if [[ $do_quiet == false ]]; then
echo "Ready."
fi
exit 0 |