Re: How to work with tabs in a bash script

From: William Park (opengeometry_at_yahoo.ca)
Date: 01/09/04


Date: 9 Jan 2004 09:40:51 GMT

William Park <opengeometry@yahoo.ca> wrote:
> those who know me have no need of my name <not-a-real-address@usa.net>
> wrote:
> > >Also, with a patched Bash which does regex,
> > > if [[ $a =~ $'([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t' ]]; then
> >
> > regex's aren't needed in this case. a standard bash is quite
> > capable, e.g.,
> >
> > IFS=$'\t' read f1 f2 f3 f4 <<<"$line"
> >
> > fields 1..3 are now in f1..f3, f4 contains whatever follows field 3.
> >
> > or using an array:
> >
> > IFS=$'\t' read -a a <<<"$line"
> >
> > fields are now in `a', i.e., ${a[0]} has the first field.
>
> Problem with IFS solution is that multiple '\t' will be skipped.

Let's see... you can do
    a=()
    array -j $'\t' a "$line"
    field1="${a[0]}"
    field2="${a[1]}"
    field3="${a[2]}"
    field4="${a[3]}"

-- 
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data management and processing.