-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_insert_totals.sh
executable file
·62 lines (57 loc) · 1.49 KB
/
copy_insert_totals.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
#!/bin/bash
Y=$1
types="recurrent nonrecurrent operating plant_equipment_works subventions capital expenditure"
for Y in `seq 2006 2013`
do
F="${Y}.csv"
csvextract.py 1 $F
done | sort -un > head_nos.tmp
while read head_no
do
head_no=`echo $head_no | sed 's/\r//g'`
SQL="INSERT INTO budget_expenditures_totals (head_no) VALUES ($head_no) ;"
#echo $SQL
#psql -h 127.0.0.1 -U scmp -c "${SQL}"
done < head_nos.tmp
for Y in `seq 2006 2013`
do
F="${Y}.csv"
HEAD=`head -1 ${F}`
line_no=0
while read line
do
if [ $line_no -le 0 ]
then
let line_no=$line_no+1
continue
fi
SQL="UPDATE budget_expenditures_totals SET "
i=3
while true
do
head_no=`echo $line | csvextract.py 1 | sed 's/\r//g'`
COL=`echo $HEAD | csvextract.py $i`
VAL=`echo $line | csvextract.py $i`
COL=`echo $COL | sed 's/\r//g'`
VAL=`echo $VAL | sed 's/\r//g'`
if [ `echo "$COL" | wc -c` -le 3 ]
then
break
fi
if [ $i -gt 3 ]
then
SQL="${SQL}, "
fi
if [ ${VAL} == '""' ]
then
VAL="NULL"
fi
SQL="${SQL} ${COL} = ${VAL}"
let i=$i+1
done
SQL="${SQL} WHERE head_no = ${head_no}"
echo $SQL
psql -h 127.0.0.1 -U scmp -c "${SQL}"
let line_no=$line_no+1
done < $F
done