bug-bash
[Top][All Lists]
Advanced

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

Re: loop through records


From: Duane Schweitzer
Subject: Re: loop through records
Date: Thu, 12 Mar 2009 08:38:02 -0500

Thanks Chris, this was very helpful. I come from an oracle background and
I'm trying to transfer some of that into bash...

Duane

On Wed, Mar 11, 2009 at 3:44 PM, Chris F.A. Johnson <cfaj@freeshell.org>wrote:

> 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]