LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   split a string into array in bash return wrong size (https://www.linuxquestions.org/questions/linux-newbie-8/split-a-string-into-array-in-bash-return-wrong-size-869958/)

xeon123 03-21-2011 06:59 AM

split a string into array in bash return wrong size
 
Hi,

I'm trying to split a string, to later iterate using a for loop like
Code:

for (( i=0; i<5; i++))
But, my script returns an array with the size 1.

Here's the script:
Code:

aver=$(grep "avg" A.txt | awk '{ print $2 }');
a=$(echo $aver | tr " " "\n");

echo ${#a[@]};

for (( i=0; i<${#a[@]}; i++))
do
    echo -e $i"\t"${a[$i]}";
done

What's wrong with the script?

sejdenfaden 03-21-2011 07:14 AM

Can you make the file A.txt or a file with the same syntax available to us?

It is hard to find out where things goes wrong without knowing how the file looks like.
If you link to a copy of the actual file remember to remove any sensitive information.

colucix 03-21-2011 07:16 AM

Same problem as here. You missed the parentheses for array assignment:
Code:

a=( $(echo $aver | tr " " "\n") )


All times are GMT -5. The time now is 06:11 PM.