bug-bash
[Top][All Lists]
Advanced

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

Re: loop through records


From: Chris F.A. Johnson
Subject: Re: loop through records
Date: Wed, 11 Mar 2009 16:44:31 -0400 (EDT)
User-agent: Alpine 1.00 (LRH 882 2007-12-20)

On Wed, 11 Mar 2009, OnTheEdge wrote:


All, I'm trying to figure out how to loop through an array of records (if
possible) and reference fields in that record, but I've only been able to
reference the entire array (array[0]) or when assigned with parens, there is
no concept of a row...

#!/bin/bash

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

      That is not an array; it is a scalar variable.

      To assign it to an array:

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

element_count1=${#array1[*]}
echo $element_count1

number_of_elements=${#array1[@]}

echo '- ARRAY-1--------------------------------'

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

set -f  ## Prevent pathname expansion, (not necessary in this example)
for REC in "${array1[@]}"
do
  set -- $REC
  printf "Field 1: %s  Field 2: %s\n" "$1" "$2"
done

--
   Chris F.A. Johnson, webmaster         <http://woodbine-gerrard.com>
   ========= Do not reply to the From: address; use Reply-To: ========
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)




reply via email to

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