-
Notifications
You must be signed in to change notification settings - Fork 0
/
blueconverter.sh
executable file
·230 lines (204 loc) · 6.67 KB
/
blueconverter.sh
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
#create initial JSON file
touch ${1%.*}.json
echo '{ "BlueButtonData" : {' > ${1%.*}.json
echo '"Conversion Data": "Converted by BlueConverter",' >> ${1%.*}.json
#read ASCII data file
blackcats="MY HEALTHEVET PERSONAL INFORMATION REPORT,DOWNLOAD REQUEST SUMMARY"
arraykeys="Contact First Name:Contacts,Provider Name:Providers,Facility Name:Facilities,Health Insurance Company:Companies,Date/Time:Appointments,Medication:Medications,Category:Medications,Allergy Name:Allergies,Medical Event:Events,Immunization:Immunizations,Test Name:Tests,Measurement Type:Measurements,Event Title:Events"
militarycategories="Regular Active Service,Reserve/Guard Association Periods,Reserve/Guard Activation Periods,Deployment Periods,DoD MOS/Occupation Codes,Military/Combat Pay Details,Separation Pay Details,Retirement Periods,DoD Retirement Pay"
servicefields="Service,Begin Date,End Date,Character of Service,Rank"
activationfields="Service,Begin Date,End Date,Activated Under"
locationfields="Service,Begin Date,End Date,Conflict,Location"
occupationfields="Service,Begin Date,Enl/Off,Type,Svc Occ Code,DoD Occ Code"
payfields="Service,Begin Date,End Date,Military Pay Type,Location"
payfields2="Service,Begin Date,End Date,Separation Pay Type"
retirementfields="Service,Begin Date,End Date,Retirement Type,Rank"
retirementpayfields="Service,Begin Date,End Date,Dsblty %,Pay Stat,Term Rsn,Stop Pay Rsn"
categoryfound=false
inArray=false
inDODArray=false
currentfieldset="$servicefields"
while read LINE
do
NLINE=$( echo $LINE | sed -e 's/.$//g') #decode line
if [[ $NLINE == *---* ]] #detect category name
then
catname=$( echo $NLINE | sed 's/\-//g' ) #remove -
catname=$( echo $catname | sed -e 's/^[ \t]*//') #remove extra spaces
#check to see if this is a category we should ignore
IFS=',' read -ra blacklist <<< "$blackcats"
categoryfound=true
for b in "${blacklist[@]}"
do
if [ "$b" = "$catname" ]
#if [ "DOD MILITARY SERVICE INFORMATION" = "$catname" ]
then
categoryfound=false
fi
done
if [[ ${#catname} > 1 ]]
then
if $categoryfound
then
if $inArray
then
echo "\"eoa\" : true } ]," >> ${1%.*}.json
fi
echo "\"eoc\" : true }, " >> ${1%.*}.json
echo "\"${catname}\" : {
" >> ${1%.*}.json
echo "Converting ${catname} ..."
inArray=false
fi
fi
else
if $categoryfound
then
#read incoming line into either key value pairs
#or determine if this should be treated like an object array
IFS=':' read -ra KEYVALUE <<< "$NLINE"
IFS=',' read -ra arrays <<< "$arraykeys" #pull arrays string to determine object arrays
if [ "$catname" = "DOD MILITARY SERVICE INFORMATION" ] #Military has completely different formatting
then
subcatname=$( echo ${KEYVALUE[0]} | sed 's/\-//g' )
subcatname=$( echo $subcatname | sed -e 's/^[ \t]*//')
if [ "${subcatname}" = "Translations of Codes Used in this Section" ] #we're done here
then
catname="END"
inDODArray=false
echo "
{\"eoa\":true }], " >> ${1%.*}.json
fi
#echo $subcatnane
IFS=',' read -ra subcats <<< "$militarycategories"
for a in "${subcats[@]}"
do
if [ "$a" = "${subcatname}" ]
then
echo ">${a}"
if $inDODArray
then
echo "{\"eoa\" : true} ],
\"${a}\" : [" >> ${1%.*}.json
else
echo " \"${a}\" : [" >> ${1%.*}.json
fi
inDODArray=true
if [ "${a}" = "${subcats[0]}" ]
then
currentfieldset=$servicefields
fi
if [ "${a}" = "${subcats[1]}" ]
then
currentfieldset=$servicefields
fi
if [ "${a}" = "${subcats[2]}" ]
then
currentfieldset=$activationfields
fi
if [ "${a}" = "${subcats[3]}" ]
then
currentfieldset=$locationfields
fi
if [ "${a}" = "${subcats[4]}" ]
then
currentfieldset=$occupationfields
fi
if [ "${a}" = "${subcats[5]}" ]
then
currentfieldset=$payfields
fi
if [ "${a}" = "${subcats[6]}" ]
then
currentfieldset=$payfields2
fi
if [ "${a}" = "${subcats[7]}" ]
then
currentfieldset=$retirementfields
fi
if [ "${a}" = "${subcats[8]}" ]
then
currentfieldset=$retirementpayfields
fi
fi
done
fi
valuelinecompressed=$(echo ${KEYVALUE[0]} | sed -e 's/^[ \t]*//')
IFS=' ' read -ra values <<< "$valuelinecompressed"
IFS=',' read -ra fieldset <<< "$currentfieldset"
if $inDODArray && [ "${values[0]}" != "Service" ] && [ "${values[0]}" != "-" ] && [ "${values[0]}" != "--" ] #data row
then
echo "{ " >> ${1%.*}.json
index=0
if [ "${values[1]}" = "Reserve" ]
then
values[0]="${values[0]} ${values[1]}"
values[1]=${values[2]}
values[2]=${values[3]}
values[3]=${values[4]}
values[4]=${values[5]}
values[5]=${values[6]}
values[6]=${values[7]}
fi
for a in "${values[@]}"
do
echo " \"${fieldset[index]}\" : \"${a}\"," >> ${1%.*}.json
index=`expr $index + 1`
done
echo " \"eor\" : true }, " >> ${1%.*}.json
fi
#else
#echo "!${catname}"
#Not Military History, continue with normal field/value split/layout
if $inArray
then
for a in "${arrays[@]}"
do
IFS=':' read -ra array <<< "$a"
if [ "${array[0]}" = "${KEYVALUE[0]}" ]
then
echo " \"eoa\":false },
{" >> ${1%.*}.json
fi
done
fi
for a in "${arrays[@]}"
do
IFS=':' read -ra array <<< "$a"
if [ "${array[0]}" = "${KEYVALUE[0]}" ]
then
if [ $inArray = false ]
then
echo " \"${array[1]}\": [ {" >> ${1%.*}.json
inArray=true
fi
fi
done
if [ ${#KEYVALUE[@]} = 2 ]
then
value=$( echo ${KEYVALUE[1]} | sed 's/\"//g' )
if [ "$catname" != "DOD MILITARY SERVICE INFORMATION" ] && [ "$catname" != "END" ]
then
if $inArray
then
echo " \"${KEYVALUE[0]}\":\"$value\"," >> ${1%.*}.json
else
echo " \"${KEYVALUE[0]}\":\"$value\"," >> ${1%.*}.json
fi
fi
else
value=$( echo ${KEYVALUE[0]} | sed 's/\"//g' )
value=$( echo ${value} | sed 's/\=//g' )
value=$( echo ${value} | sed 's/\-//g' )
if [ "$value" != "" ] && [ "$catname" != "DOD MILITARY SERVICE INFORMATION" ] && [ "$catname" != "END" ]
then
echo " \"Note\":\"$value\"," >> ${1%.*}.json
fi
fi
fi
fi
done < $1
echo '"eoc": true
}, "eof" : true }' >> ${1%.*}.json
echo "${1%.*}.json written"
echo "Completed"