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
| #!/bin/sh
#
#Initialisation
table="Formation"
HOME=/home/adullact/Formation
HOSTNAME=`hostname`
file=$HOME/../Formation.txt
GENERATION=$HOME/Fichiers
if [ ! -d $HOME ];then
mail -s "Erreur Import $table $HOSTNAME" sabrina.toulouse@adullact-projet.coop <<-EOM
`date`
Erreur : l'arborescence a ete modifiee et ne correspond plus au script.
EOM
exit 1
fi
if [ ! -f $file ];then
mail -s "Erreur Import $table $HOSTNAME" sabrina.toulouse@adullact-projet.coop <<-EOM
`date`
Erreur : le fichier .txt n'existe pas ou n'est pas au bon emplacement.
EOM
exit 1
fi
# variables
pompier1="1"
pompier2="2"
id=0
export id
utf8=$HOME/tmp/lutece.utf8
formationtri=$HOME/tmp/lutece.tri
#Conversion en UTF8
if (file $file |grep 8859 >/dev/null); then
iconv --from=ISO-8859-15 --to=UTF-8 2>/dev/null < $file > $utf8
file="$utf8"
fi
#Traitement, stockage et suppression de la 1e ligne
head=$(head -n 1 $file |sed -e "s/ /_/g" -e "s/^/INSERT INTO \`$table\`\(\`/" -e "s/;/\`,\`/g" -e "s/$/\`\) VALUES/" -e "s/'/\\\`/g" -e "s/[éêèë]/e/g" -e "s/[ùü]/u/g" -e "s/ç/c/g" -e "s/à/a/g")
sed 1d $file | sort -b -f -i -u > $formationtri
file="$formationtri"
#Traitement des quotes
sed -i "s/'/ /g" $file
#Boucle
cat $file | while read line
do
pompier2="`echo $line |cut -d ";" -f 4`"
pompier2all=`echo $line | sed -e "s/^/\('/" -e "s/[éêèë]/e/g" -e "s/[ôö]/o/g" -e "s/[ùü]/u/g" -e "s/ç/c/g" -e "s/à/a/g" -e "s/#EMPTY//g" -e "s/ 00:00:00//g" -e "s/;/','/g" -e "s/$/'\),/" -e "/''/s|'','\([^,]*\)'),$|'00/00/0000','\1'),|" -e "/''/s|'','\(.*\)'),$|'-','\1'),|"`
if (test "$pompier1" = "$pompier2")
then
echo $pompier2all >>$GENERATION/$table.$id.sql
else
[ -f $GENERATION/$table.$id.sql ] && sed -i '$s/,$/;/' $GENERATION/$table.$id.sql
id=$(( $id + 1 ))
echo $head > $GENERATION/$table.$id.sql
echo $pompier2all >>$GENERATION/$table.$id.sql
pompier1=$pompier2
fi
done
count=$(ls -l $GENERATION/$table.*.sql |wc -l)
sed -i '$s/,$/;/' $GENERATION/$table.$count.sql
rm -f $utf8 $formationtri |
Partager