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-17-2011, 10:36 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Rep: Reputation: 16
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?
 
Old 03-17-2011, 10:45 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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.
Old 03-17-2011, 10:46 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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.
Old 03-17-2011, 10:48 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
Old 03-17-2011, 10:56 AM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
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.
 
Old 03-17-2011, 11:03 AM   #6
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Original Poster
Rep: Reputation: 16
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?
 
Old 03-17-2011, 11:13 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Quote:
Am I missing something?
Yes
Code:
arr=($(echo $m | tr " " "\n"))
 
1 members found this post helpful.
Old 03-17-2011, 11:14 AM   #8
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Original Poster
Rep: Reputation: 16
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
 
Old 03-17-2011, 11:16 AM   #9
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Original Poster
Rep: Reputation: 16
thanks grail, it worked.
 
  


Reply

Tags
array, assignment, bash, variable


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-19-2011 11:01 PM
[SOLVED] how to split string into an array/list of lines in C++? rohedin Programming 11 06-06-2010 10: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 12:49 PM
[SOLVED] split files by specifying a string (bash shell) vikas027 Programming 12 11-01-2007 06:46 AM
Split a string on newlines (bash) rose_bud4201 Programming 7 04-14-2005 01:58 PM


All times are GMT -5. The time now is 02:22 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration