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
| #!/bin/sh
touch log.log
echo "$(date +%X) ### Debut programme ###" >> log.log
rm tmp.txt
echo "$(date +%X) - Debut formattage fichiers a traiter" >> log.log
ls ./*T_*.txt | while read fic
do
#on enleve les espaces du nom du fichier et on remplace par _
if [ `echo "$fic"|grep ' '|wc -l` -ne 0 ]
then
mv "$fic" `echo "$fic" | sed -r 's/\s+/_/g'`
nomFic=`echo "$fic" | sed -r 's/\s+/_/g'`
#on remplace les tabulations par une virgule et on met chaque valeur entre simple quote
sed -i "s/^/'/; s/\t/','/g; s/$/'/" $nomFic
fi
done
echo "$(date +%X) - Fin formattage fichiers a traiter" >> log.log
echo "$(date +%X) - Debut generation .SQL" >> log.log
find . -type f -name "*T_*.txt" | while read fichier
do
nomTable=$(echo "$fichier" | gawk -F ',' '{sub("Reprise_", "", $1);gsub("[^A-Z_]","", $1);print table=$1}')
flux=$(echo "$fichier" | gawk -F ',' '{sub("^_", "", $2);print flux=$2}')
colonnes=""
ts=$(date +%s)
echo "$(date +%X) - Debut traitement ==> $fichier" >> log.log
i=0
echo "fichier en cours =>"$fichier
nbLignes=$(sed -n '$=' $fichier)
echo "nb=>"$nbLignes
j=0
while read ligne
do
if test $j = 10000
then
ts=$(date +%s)
touch ${nomTable}_${flux}_${ts}_$i.sql
cat tmp.txt >> ${nomTable}_${flux}_${ts}_$i.sql
rm tmp.txt
touch tmp.txt
j=0
fi
if test $i = 0
then
colonnes=$(echo "$ligne" | gawk '{gsub("\047", "", $0);print colonnes=$0}')
else
#on transforme les timestamp par la fonction oracle TO_TIMESTAMP_TZ()
ligne3=$(echo "$ligne" | sed "s#\('[0-9]\{1,2\}/[0-9]\{1,2\}/[0-9]\{4\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\)\.[0-9]\{6\} \(+[0-9]\{2\}:[0-9]\{2\}'\)#to_timestamp_tz(\1 \2,'dd/mm/yyyy hh24:mi:ss TZH:TZM')#g")
echo "insert into ${nomTable} ($colonnes) values ( ${ligne3} );" >> tmp.txt
fi
i=$(($i+1))
j=$(($j+1))
done < $fichier
cat tmp.txt >> ${nomTable}_${flux}_${ts}_$i.sql
rm tmp.txt
echo "$(date +%X) - Fin traitement ==> $fichier" >> log.log
mv *.sql sql/
done
echo "$(date +%X) - Fin generation .SQL" >> log.log
echo "$(date +%X) - Debut creation script d'appel global" >> log.log
touch sql/globalScript.sql
find sql/*.sql -type f | while read script
do
echo "@${script}" >> sql/globalScript.sql
done
mv sql/globalScript.sql globalScript.sql
echo "$(date +%X) - Fin creation script d'appel global" >> log.log
# sqlplus /nolog
# connect user/pass
# @globalScript.sql
# exit |
Partager