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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-17-2011, 10:36 AM
|
#1
|
|
Member
Registered: Sep 2006
Posts: 342
Rep:
|
split a string into array in bash
how do I split a string into an array?
In this string:
"this is a story"
how do I split it by the space?
|
|
|
|
03-17-2011, 10:45 AM
|
#2
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
Something like this should work:
Code:
#!/usr/bin/bash
IN="this is a story"
arr=$(echo $IN | tr " " "\n")
for x in $arr
do
echo "\"$x\""
done
It should print:
Code:
"this"
"is"
"a"
"story"
|
|
|
0 members found this post helpful.
|
03-17-2011, 10:46 AM
|
#3
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,314
|
Code:
array=($(echo "this is a story"))
Edit: As it seems I was a little slow I will make an addendum to Snark's entry, if you assign the string to a variable the echo is not required at all:
Code:
IN="this is a story"
arr=($IN)
Last edited by grail; 03-17-2011 at 10:49 AM.
|
|
|
0 members found this post helpful.
|
03-17-2011, 10:48 AM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
The blank space, which is part of the IFS shell variable, is used by default in arrays assignment:
Code:
string="this is a story"
array=($string)
Look at http://tldp.org/LDP/abs/html/arrays.html for details.
|
|
|
1 members found this post helpful.
|
03-17-2011, 10:56 AM
|
#5
|
|
Member
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Rep: 
|
Is this what you want?
Code:
~$ myArray=(this is a story)
~$ echo ${myArray[0]}
this
~$ echo ${myArray[1]}
is
~$ echo ${myArray[2]}
a
~$ echo ${myArray[3]}
story
~$
Bash Reference Manual, 6.7 Arrays
Edit
Three replies before I submitted my post. I'm so slow :P
Last edited by Telengard; 03-17-2011 at 10:59 AM.
|
|
|
|
03-17-2011, 11:03 AM
|
#6
|
|
Member
Registered: Sep 2006
Posts: 342
Original Poster
Rep:
|
My problem is that solution that you guys put here doesn't work with me and I can't understand why.
Code:
m=$(grep TOTAL_MAPS $AVERAGEFILES | awk '{ print $2}');
echo 1 - $m
arr=$(echo $m | tr " " "\n")
echo 2 - ${arr[0]}
The output is:
Code:
1 - 90 180 90 120 120
2 - 90 180 90 120 120
As you can see, the string isn't split into an array.
Am I missing something?
|
|
|
|
03-17-2011, 11:13 AM
|
#7
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,314
|
Yes
Code:
arr=($(echo $m | tr " " "\n"))
|
|
|
1 members found this post helpful.
|
03-17-2011, 11:14 AM
|
#8
|
|
Member
Registered: Sep 2006
Posts: 342
Original Poster
Rep:
|
Here's the result with set -x enabled:
Code:
+ m='90
180
90
120
120'
+ echo 1 - 90 180 90 120 120
1 - 90 180 90 120 120
++ echo 90 180 90 120 120
++ tr ' ' '\n'
+ arr='90
180
90
120
120'
+ echo 2 - 90 180 90 120 120
2 - 90 180 90 120 120
+ set +x
|
|
|
|
03-17-2011, 11:16 AM
|
#9
|
|
Member
Registered: Sep 2006
Posts: 342
Original Poster
Rep:
|
thanks grail, it worked.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:29 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|