LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-11-2005, 06:36 AM   #1
basher400
Member
 
Registered: Mar 2005
Posts: 54

Rep: Reputation: 15
splitting strings


hi

this must be one of the easiest ones ever:
I have a string: "danyXmarryXbarry"

and I would like to split it into parts, "X" as the delimiter, so that I will have this:
$a[0]="dany"
$a[1]="marry"
$a[2]="barry"

isn't there something simply as the split function in Perl to do it? without getting onto complicated regex sed commands?
 
Old 04-11-2005, 07:37 AM   #2
basher400
Member
 
Registered: Mar 2005
Posts: 54

Original Poster
Rep: Reputation: 15
i can't believe no one knows the answer to this
 
Old 04-11-2005, 07:40 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
What language?
 
Old 04-11-2005, 07:43 AM   #4
basher400
Member
 
Registered: Mar 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by theYinYeti
What language?
bash programing
ssh
 
Old 04-11-2005, 08:28 AM   #5
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
This should do it:-
Code:
str="danyXmarryXbarry"

a=($(echo $str | tr "X" " "))
 
Old 04-12-2005, 01:31 AM   #6
basher400
Member
 
Registered: Mar 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by ahh
This should do it:-
Code:
str="danyXmarryXbarry"

a=($(echo $str | tr "X" " "))
thanks.
now just tell me, where was the last part, "barry", stored?
$a is now "dany"
there is no $a[2], $a{2}, or $a(2)....
 
Old 04-12-2005, 02:24 AM   #7
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
To reference arrays in bash you have to use the format "${a[x]}"

So in this example we get:-
Code:
ahh@desktop ~ $ str=dannyXmarryXbarry;  a=($(echo $str | tr "X" " ")); echo ${a[0]}
danny
ahh@desktop ~ $ str=dannyXmarryXbarry;  a=($(echo $str | tr "X" " ")); echo ${a[1]}
marry
ahh@desktop ~ $ str=dannyXmarryXbarry;  a=($(echo $str | tr "X" " ")); echo ${a[2]}
barry
 
  


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
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
splitting strings marri Programming 5 04-08-2005 05:33 PM
Splitting a File Slayer097 Linux - Newbie 4 02-22-2005 07:15 PM
C: pointers, strings, splitting headache notsoevil Programming 5 06-19-2003 04:04 PM
Splitting my 6o Madmanator Linux - Newbie 1 01-03-2002 11:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:10 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
Open Source Consulting | Domain Registration