LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2011, 07:59 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
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?
 
Old 03-21-2011, 08:14 AM   #2
sejdenfaden
LQ Newbie
 
Registered: Mar 2011
Location: Denmark, Aarhus
Distribution: Fedora 14 64-bit
Posts: 4

Rep: Reputation: 1
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.
 
Old 03-21-2011, 08:16 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Same problem as here. You missed the parentheses for array assignment:
Code:
a=( $(echo $aver | tr " " "\n") )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script split array robertngo Programming 13 06-20-2011 12:01 AM
[SOLVED] split a string into array in bash xeon123 Linux - Newbie 8 03-17-2011 12:16 PM
[SOLVED] how to split string into an array/list of lines in C++? rohedin Programming 11 06-06-2010 11:54 AM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 01:49 PM
Split a string on newlines (bash) rose_bud4201 Programming 7 04-14-2005 02:58 PM


All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration