Assigning values for a dynamic array for an input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning values for a dynamic array for an input
# 1  
Old 12-09-2009
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
export TEMP="abc1,abc2,abc3,abc4"
echo $TEMP_NODE | awk -F"," ' { arr[0]=$1 } { arr[1]=$2 } { arr[2]=$3 } { arr[3]=$4 }'

arr[0]=abc1
arr[1]=abc2
arr[2]=abc3
arr[3]=abc4

But If I give the input as abc1,abc2,.......,abc100, all these abc* strings should be assigned to an array of size 100. And if I give the input as abc1,abc2, it should assign the strings (abc1 and abc2) to an array of size 2.

With my little knowledge in Unix, I tried to get a solution by using different combinations cut,awk,sed. But I'm unable to get the desired result.Smilie

Request the Unix gurus to help me with a small shell script (sh/bash) or some thoughts to achieve this requirement.

Thanks
~Suneel

Last edited by suneelj; 12-09-2009 at 04:45 PM..
# 2  
Old 12-09-2009
Is this what you mean?

Code:
TEMP=abc1,abc2,abc3,abc4

echo $TEMP | nawk -F, '
{
        for ( i = 1; i <= NF; i++ )
                arr[i] = $i
 }END{
        for ( i = 1; i <= NF; i++ )
                print arr[i]
 }'

Output: -

Code:
abc1
abc2
abc3
abc4

# 3  
Old 12-10-2009
Thanks steadyonabix. This worked for me upto certain level as I want to use that array in outside of the (n)awk block {}.

Meanwhile after struggling 3 hours I found another way as below.
read TEMP_NODE
echo ""
ARR_NODE=$(echo $TEMP_NODE | tr "," "\n")
for x in $ARR_NODE
do
echo "> [$x]"
done

Anyways thanks for the help.

Regards
~Suneel
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

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

3. 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

4. Shell Programming and Scripting

Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables? I am assigning a variable name to --> $user_var Then I am trying to change its underlying variable value by $((user_var))=$user_value .. its failing,, Please let me know if there is a way to do this dynamically.. FileA.props... (5 Replies)
Discussion started by: kchinnam
5 Replies

5. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies

6. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(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... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

7. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 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