[kwlug-disc] bash question tab separated values

Raul Suarez rarsa at yahoo.com
Fri Nov 6 08:26:44 EST 2009


An alternative is to read the values to an array.

oldifs="${IFS}"
IFS="<TAB>"
while read -a myArray
do
  #do whatever with the array. i.e.
  echo ${myArray[1]}
done < myfile.txt
IFS="${oldifs}"

Be sure to configure your editor to save tabs as tabs, not as spaces

Raul Suarez

Technology consultant
Software, Hardware and Practices
_________________
http://rarsa.blogspot.com/ 
An eclectic collection of random thoughts


--- On Thu, 11/5/09, Robert P. J. Day <rpjday at crashcourse.ca> wrote:

> From: Robert P. J. Day <rpjday at crashcourse.ca>
> Subject: Re: [kwlug-disc] bash question tab separated values
> To: "KWLUG discussion" <kwlug-disc at kwlug.org>
> Received: Thursday, November 5, 2009, 11:37 PM
> On Thu, 5 Nov 2009, Khalid Baheyeldin
> wrote:
> 
> >
> > On Thu, Nov 5, 2009 at 2:46 PM, Richard Weait <richard at weait.com>
> wrote:
> >       I'm reading lines from
> a tab-separated-value text file.  Ten values
> >       per line.  I'd like to
> load them into ten variables for further
> >       processing, then
> output.
> >
> >       It ain't working for
> me.  Looks like the tabs are being silently
> >       dropped then my cut
> -f3, for example, returns the complete line.
> >
> >       Some fields include
> spaces, so I'd rather continue to split on
> >       tabs.
> >       How do I keep the tabs?
>  How should I be reading these lines?
> >
> >
> > Can you process in awk?
> >
> > If so, then just use:
> >
> > awk -F"\t" '{print $1; ... something else; }'
> yourfile.txt
> >
> > If you still insist on doing it in shell, then do
> this:
> >
> > exec 3>&1 < file.txt
> > while true
> > do
> >   read LINE
> >   if [ "$?" != 0 ]; then  # It is an End Of File
> condition
> >     break
> >   fi
> >
> >   FIELD0=`echo "$LINE" | awk -F"\t" '{print $1}'`
> >   FIELD1=`echo "$LINE" | awk -F"\t" '{print $2}'`
> >   FIELD2=`echo "$LINE" | awk -F"\t" '{print $2}'`
> >
> >   echo $FIELD0
> >   echo $FIELD2
> > done
> > exec <&3 3<&-
> 
>   can't you just set the internal field separator
> before reading in
> the file in a bash loop:
> 
>   IFS="<TAB>"
>   while read v1 v2 v3 ...etc... v10 ; do
>     ... whatever with v1 v2 v3 ...
>   done < inputfile
> 
>   the proper way to set the IFS would be (from
> experience), typing the
> first quote, then a ^v, followed by pressing the TAB key,
> then
> finishing off with the closing quote.  the ^v means
> "take the next
> character absolutely literally."
> 
>   i just tested that and it seems to work.
> 
> rday
> --
> 
> ========================================================================
> Robert P. J. Day           
>                
>    Waterloo, Ontario, CANADA
> 
>             Linux Consulting,
> Training and Kernel Pedantry.
> 
> Web page:             
>                
>             http://crashcourse.ca
> Twitter:             
>                
>          http://twitter.com/rpjday
> ========================================================================
> -----Inline Attachment Follows-----
> 
> _______________________________________________
> kwlug-disc_kwlug.org mailing list
> kwlug-disc_kwlug.org at kwlug.org
> http://astoria.ccjclearline.com/mailman/listinfo/kwlug-disc_kwlug.org
> 


      __________________________________________________________________
Get the name you've always wanted @ymail.com or @rocketmail.com! Go to http://ca.promos.yahoo.com/jacko/




More information about the kwlug-disc mailing list