Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 9th, 2010, 3:23 PM   #1
danMyung
Newbie
 
Join Date: Sep 2010
Posts: 2
Rep Power: 0 danMyung is on a distinguished road
Blank spaces

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:

    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.
danMyung is offline   Reply With Quote
Old Sep 9th, 2010, 5:23 PM   #2
scottn
Hobbyist Programmer
 
scottn's Avatar
 
Join Date: Sep 2010
Location: der Schweiz
Posts: 146
Rep Power: 5 scottn is on a distinguished road
Re: Blank spaces

Hi.

The first thing I would change is this:

for linea in `/bin/cat $1`
  ...
  ...
done

to:

while read lines; do
  ...
  ...
done < $1

A for-loop reads a word at a time, not a line at a time, as the shell man-page describes:

for name [ in word ] ; do list ; done
              The  list  of  words  following in is expanded, generating a list of items.  The variable name is set to each element of this list in turn, and list is executed each time.  If the in word is omitted,  the  for command executes list once for each positional parameter that is set (see PARAMETERS below).  The return status is the exit status of the last command that executes.  If the expansion of the items following in results in an empty list, no commands are executed, and the return status is 0.

Although you could probably get away with this:
IFS=""
for linea in `/bin/cat $1`
  ...
  ...
done
the while-loop approach is the better method.

Using cat, etc. to feed the for-loop argument list is OK for small files, but with larger files, you'll run into problems (arg list too long, etc.).
scottn is offline   Reply With Quote
Old Sep 10th, 2010, 11:02 AM   #3
danMyung
Newbie
 
Join Date: Sep 2010
Posts: 2
Rep Power: 0 danMyung is on a distinguished road
Re: Blank spaces

Thanks for your answer!

I tried with
Quote:
IFS=""
and it works fine only with files that contain one single line, i guess i'll have to try with while-loop..

danMyung is offline   Reply With Quote
Old Sep 10th, 2010, 11:10 AM   #4
scottn
Hobbyist Programmer
 
scottn's Avatar
 
Join Date: Sep 2010
Location: der Schweiz
Posts: 146
Rep Power: 5 scottn is on a distinguished road
Re: Blank spaces

Quote:
Originally Posted by danMyung View Post
it works fine only with files that contain one single line, i guess i'll have to try with while-loop..
Ha ha. You're right (knew I shouldn't have added that useless information ).

Go with the while-loop!
scottn is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
When i download files onto my computer they're blank and wont do anything please help? kutti2020 Coder's Corner Lounge 0 Jul 18th, 2010 11:47 PM
Finding Dates and Blank Spaces in AWK sjkiss@gmail.com Sed and Awk 1 Jun 28th, 2010 2:23 PM
Spaces in path names estergon Bash / Shell Scripting 13 Mar 25th, 2008 2:55 PM
Removing double spaces with str_replace davil PHP 2 Dec 12th, 2007 5:19 PM
another string problem trim spaces lucifer C# 1 Dec 2nd, 2005 10:01 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC