-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvToSQL.sh
More file actions
executable file
·66 lines (58 loc) · 2.21 KB
/
csvToSQL.sh
File metadata and controls
executable file
·66 lines (58 loc) · 2.21 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
#!/bin/bash
# This script is part of the MerlinMake repository
# Copyright (C) 2023 CoderMerlin.Academy
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Halt on errors and undefined environment variables
set -eu
# Validate parameters
if [ $# != 3 ]
then
echo -e "Usage: $0 <csvPathname> <templatePathname> <outputPathname>"
echo -e "\tThe first line of the csv file MUST contain a comment containing the field names, e.g."
echo -e "\t\t# name,description,source,target"
exit 1
fi
csvPathname=$1
templatePathname=$2
outputPathname=$3
if [ ! -f "$csvPathname" ]
then
echo "The specified csv pathname '${csvPathname}' could not be found."
exit 2
fi
if [ ! -f "$templatePathname" ]
then
echo "The specified template pathname '${templatePathname}' could not be found."
exit 2
fi
rm -f "$outputPathname"
# Read the first line of the csv file to determine variable names for replacement
fieldCount=$(csvstat -n "$csvPathname" | wc -l)
fieldNames=()
for fieldIndex in $(seq 1 $fieldCount)
do
fieldName=$(head -n 1 "$csvPathname" | sed 's/^#[ \t]*//' | csvcut --skipinitialspace -c $fieldIndex)
fieldNames+=("$fieldName")
echo "Field[$fieldIndex] -> $fieldName"
done
echo
# The template file should produce valid SQL after any specified
# environment variables have been replaced
while read -r line
do
for fieldIndex in $(seq 0 $((fieldCount-1)) )
do
fieldValue=$(echo $line | csvcut --skipinitialspace -c $((fieldIndex+1)) | sed -e 's/^"\(.*\)"$/\1/')
declare -x "${fieldNames[$fieldIndex]}=${fieldValue}"
done
envsubst < "$templatePathname" | tee --append "$outputPathname"
done < <(tail -n +2 "$csvPathname")