Hi everyone, i'm kinda new into bash scripting so i was wondering if anyone can help me...
I have a text file that contains several lines, like this:
305699991234567QWER12345678 34500000IQ4RETRET34540909 201007:19:02M
305699991234567QWER12345678 34500000IQ4RETRET34540909 201007:19:02M
305699991234567QWER12345678 34500000IQ4RETRET34540909 201007:19:02M
My script it's supposed to read each line, take certain characters and insert them into a database.
I'm using "/bin/cut -c 16-28" (for example) to take the specific characters that will be the fields for my table.
It seems to be working fine but the problem comes when the lines of the text file have blank spaces " ", i can“t save anything that is next to the blank space.
Is there a way to read the whole line including the blank spaces?
This is my code so far:
Code:
dirtmp=/home/danielm/Shellsconpass/tmp
dirtmpsql=/home/danielm/Shellsconpass/tmp
ubicacion_arch=/home
respaldo_arch=/saai/archivos/bancos_dc
dircfg=/saai/etc
patente=`/bin/echo $1 | /bin/cut -c 2-5`
envio=`/bin/echo $1 | /bin/cut -c 6-8`
juliano=`/bin/date +%j`
anho=`/bin/date +%y`
semana=`/bin/date +%V`
Host=`/bin/grep "Host:" $dircfg/Param_BDBancos.cfg | /bin/cut -c 6-`
User=`/bin/grep "User:" $dircfg/Param_BDBancos.cfg | /bin/cut -c 6-`
Password=`/bin/grep "Password:" $dircfg/Param_BDBancos.cfg | /bin/cut -c 10-`
Database=`/bin/grep "Database:" $dircfg/Param_BDBancos.cfg | /bin/cut -c 10-`
Puerto=`/bin/grep "Puerto:" $dircfg/Param_BDBancos.cfg | /bin/cut -c 8-`
cd $ubicacion_arch/aaa$patente
fecha=`/bin/date -r $1 +%F`
hora=`/bin/date -r $1 +%T`
if `/bin/cut -c 1-2 $1 | /bin/grep -q "30"`
then
for linea in `/bin/cat $1`
do
tipo_lin=`/bin/echo $linea | /bin/cut -c 1-2`
case $tipo_lin in
30)
aduana=`/bin/echo $linea | /bin/cut -c 3-4`
patente=`/bin/echo $linea | /bin/cut -c 5-8`
pedimento=`/bin/echo $linea | /bin/cut -c 9-15`
rfc=`/bin/echo $linea | /bin/cut -c 16-28`
pto_origen=`/bin/echo $linea | /bin/cut -c 29-30`
operacion_bancaria=`/bin/echo $linea | /bin/cut -c 31-40`
firma=`/bin/echo $linea | /bin/cut -c 41-50`
f1=`/bin/echo $linea | /bin/cut -c 51`
f2=`/bin/echo $linea | /bin/cut -c 52`
f3=`/bin/echo $linea | /bin/cut -c 53`
f4=`/bin/echo $linea | /bin/cut -c 54`
f5=`/bin/echo $linea | /bin/cut -c 55`
f6=`/bin/echo $linea | /bin/cut -c 56`
f7=`/bin/echo $linea | /bin/cut -c 57`
f8=`/bin/echo $linea | /bin/cut -c 58`
fecha="$f5$f6$f7$f8-$f3$f4-$f1$f2"
hora=`/bin/echo $linea | /bin/cut -c 59-66`
turno=`/bin/echo $linea | /bin/cut -c 67`
fuente=$respaldo_arch/sem$anho$semana\_$juliano/[eE]`/bin/echo $1 | /bin/cut -c 2-8`.$juliano
if [ -f $fuente ]
then
hora_envio=`/bin/date -r $fuente +%T`
hora_respuesta=`/bin/date -r $1 +%T`
/bin/echo "select timediff('$fecha $hora_respuesta','$fecha $hora_envio') 'tiempo'" >$dirtmpsql/Q$1.sql
tiempodif=`/usr/bin/mysql < $dirtmpsql/Q$1.sql`
for lineaa in `/bin/echo $tiempodif`
do
letra_lin=`/bin/echo $lineaa | /bin/cut -c 3`
if [ $letra_lin = ":" ]
then
horareal=`/bin/echo $lineaa | /bin/cut -c 1-8`
fi
done
hora_maestra=`/bin/echo "$horareal"`
/bin/rm $dirtmpsql/Q$1.sql
hora_envio=`/bin/date -r $fuente +%T`
/bin/echo " insert into firmas_pago_electronico values('$patente','$pedimento','$envio','$fecha','$hora'," > $dirtmp/Q$1.sql
/bin/echo " '$hora_envio','$hora_maestra','$aduana','$rfc','$pto_origen','$operacion_bancaria','$firma','$turno')" >> $dirtmp/Q$1.sql
/usr/bin/mysql -h $Host -u $User -p$Password -D $Database -P $Puerto < $dirtmp/Q$1.sql
/bin/rm $dirtmp/Q$1.sql
fi
;;
esac
done
fi
Thanks for your time.
