Assigning values to an array via for/while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning values to an array via for/while loop
# 1  
Old 11-24-2008
Assigning values to an array via for/while loop

I need to do something like this:
Code:
for i in 1 2 3 4 5; do
       arr[$i]=$(awk 'NR="$i" { print $2 }' file_with_5_records)
done

That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop.

Is my syntax wrong? Is there a trick? I can't seem to get the values assigned properly, does the shell forget them when it exits the loop? Is this even possible?
# 2  
Old 11-24-2008
Code:
for i in 1 2 3 4 5; do
      arr[$i]=$(awk 'NR=="$i" { print $2 }' file_with_5_records)
done

Got that far.

So YES: my syntax was incorrect to begin with. I've been dabbling with programming so little that I still have trouble remembering that = is an assignment and not a comparison.

The script still doesn't do what I want because AWK doesn't know the value of $i. I need to pass it to AWK somehow, or come up with a different approach.

Any tips/workarounds...??

I'm really looking forward to getting stuck at some more profound obstacles...

# 3  
Old 11-24-2008
Bug

Figured it out.

Code:
for i in 1 2 3 4 5; do
        arr[$i]=$(awk 'NR=='"$i"' { print $2 }' examplefile)
        print ${arr[$i]}
done

Testing...

$ cat examplefile
skipme printme1 skipmetoo
skipme printme2 skipmetoo
skipme printme3 skipmetoo
skipme printme4 skipmetoo
skipme printme5 skipmetoo

$ for i in 1 2 3 4 5; do
> arr[$i]=$(awk 'NR=='"$i"' { print $2 }' examplefile)
> print ${arr[$i]}
> done
printme1
printme2
printme3
printme4
printme5



Use the shell variable wrapped in double quotes inside single quotes to pass it to awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Assigning values to 2 variables within for loop

Hi All, Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently: if then for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$") do InFile=$dirA/$fname ... (4 Replies)
Discussion started by: swasid
4 Replies

4. Shell Programming and Scripting

Assigning Column Values to ARRAY in ksh

Hi , i have file which is having two fields in it (#delimited) ABC#FILE_01.DAT DEF#FILE_02.DAT i want to write first field values to one array example A_01 and second field values to B_02 array please let me know how to do this ,my final requirement i have send out a mail for each record... (2 Replies)
Discussion started by: kkabc789
2 Replies

5. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
6 Replies

6. Shell Programming and Scripting

[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer. I have input files containing something like File 1 Apples Apples Apples Apples File 2 Bananas Bananas Bananas Bananas (4 Replies)
Discussion started by: hubleo
4 Replies

7. Shell Programming and Scripting

Assigning values for a dynamic array for an input

Hello, Can somebody please give me a snippet for the below requirement. I want to assign the values separeted by a comma to be assigned to a dynamic array. If I give an input (read statement) like abc1,abc2,abc3,abc4,abc5, all these strings abc* should be assigned to an array like below... (2 Replies)
Discussion started by: suneelj
2 Replies

8. Shell Programming and Scripting

Assigning the values to an Array

hi every body, i donot know how to assign a array varible with a file see i having file more file property1 Name property2 Address the above two line are tab Space seperated between the property and its value i want to seperate it and assign to... (1 Reply)
Discussion started by: kkraja
1 Replies

9. Shell Programming and Scripting

perl: Assigning array values..

I have to add a variable value to an array, something like this: ...... @my_array_name = $value_of_this_variable; This doesnt seem to work, any ideas why? Thanks! (4 Replies)
Discussion started by: looza
4 Replies

10. UNIX for Dummies Questions & Answers

Assigning values to an array

The way I've been using arrays currently have been: #!/bin/ksh set -A myArray myArray=value1 myArray=value2 myArray=value3 myArray=value4 Is there a way I can assign values to an array that will automatically place the value into the next element in the array like: myArray=value1... (4 Replies)
Discussion started by: yongho
4 Replies
Login or Register to Ask a Question