bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: loop through records


From: Greg Wooledge
Subject: Re: loop through records
Date: Wed, 11 Mar 2009 16:40:11 -0400
User-agent: Mutt/1.4.2.2i

On Wed, Mar 11, 2009 at 01:11:51PM -0700, OnTheEdge wrote:
> array1="187431346 0323 mirrored 11866
> 187431346 0324 mirrored 11866
> 187431346 0325 mirrored 11866
> 187431346 0326 mirrored 11866"

That's not an array.  It's just a big string.

array1=("187431346 0323 mirrored 11866"
        "187431346 0324 mirrored 11866"
        "187431346 0325 mirrored 11866"
        "187431346 0326 mirrored 11866")

is an array, although whether that's the approach you really want is
still an open question at this point.

> for REC in "${array1[*]}"
> do
>   echo "Field 1: ${REC[0]}  Field 2: ${REC[1]}"
> done
> 
> I would like to see something like this:
> Field 1: 187431346   Field 2: 0323
> Field 1: 187431346   Field 2: 0324
> Field 1: 187431346   Field 2: 0325
> Field 1: 187431346   Field 2: 0326

I suspect you want multiple arrays.  You could populate them from the
first one if you wish:

i=0
for a in "${array1[@]}"; do
  read 'f1[i]' 'f2[i]' 'f3[i]' 'f4[i]' <<< "$a"
  let i++
done

Then:

for i in ${!f1[*]}; do
  printf "Field 1: %8s  Field 2: %s\n" "${f1[i]}" "${f2[i]}"
done

Although of course there's more than one way to do all this.  Iterating
through the four words of each element of a single array would work,
but might be a bit cumbersome.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]