LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2010, 05:54 PM   #1
praveenhm
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Rep: Reputation: 0
Bash string splitting into array


I am using gnu bash 3.2
I need to split the string into array like

a=this_is_whole_world.file # split [_.]

I need to split this on _ and . some thing like this
a[0]=this
a[1]=is
a[2]=whole
a[3]=world
a[4]=file

preferable using bash regex. if not sed is also ok.

Thanks
Pen
 
Old 10-14-2010, 06:01 PM   #2
swingliner
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Rep: Reputation: 2
http://www.unix.com/shell-programmin...tion-bash.html
 
Old 10-14-2010, 06:43 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
IFS="_"
set -- $a
array=( $@ )
 
1 members found this post helpful.
Old 10-15-2010, 12:04 AM   #4
praveenhm
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
String splitting

thanks ghost,

But i still did't get it, can you more details, and also i need split on both underscore_ and dot.

like one_two_three.four

one
two
three
four

thanks
Pen
 
Old 10-15-2010, 12:16 AM   #5
praveenhm
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks ghost
i got it, here is my final script,

a="one_two_three_four.five"
IFS="_."
set -- $a
array=( $@ )

echo ${#array[@]} # length of array
echo ${array[0]} #
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
echo ${array[4]} #five
 
Old 10-15-2010, 01:47 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
i'd prefer to use read for that:
Code:
IFS=...
read -a VAR <<< "$STRING"
assuming:
Code:
[[ BASH_VERSINFO -ge 3 ]]
 
Old 10-15-2010, 09:13 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
How about
Code:
IFS='_.'
array=($(echo this_is_whole_world.file))
unset IFS
It works when the words between _ or . delimiters contain spaces too.

When IFS is unset, bash behaves as if it had the default value so unsetting it is a neater solution than saving it and restoring it unless it may be set to a non-default value.

EDIT: tested on bash version 3.1.17
 
Old 10-15-2010, 11:00 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well if it comes down to playing with IFS I would go for:
Code:
IFS='_.' read -r -a ARR <<< "$string"
This way the IFS change is localized to the action and no need to check or change back
 
2 members found this post helpful.
Old 10-15-2010, 01:46 PM   #9
praveenhm
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Grail, your solution looks idle,
Thanks catkin and Konsolebox giving soulution.

Pen
 
Old 10-16-2010, 03:46 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
No probs ... always more than one way to skin a cat so I am learning everyday

Please mark as SOLVED if you have your solution.
 
Old 10-16-2010, 08:33 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by grail View Post
This way the IFS change is localized to the action and no need to check or change back
Nice improvement grail
 
Old 10-19-2010, 06:39 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Nice one grail.

Now I no longer have to use a wrapper function. Or maybe now just optional.
 
Old 10-25-2010, 08:01 AM   #13
Gussy
LQ Newbie
 
Registered: Sep 2006
Posts: 2

Rep: Reputation: 0
Code:
User@term$ func() { i=0; while [[ $i != ${#1} ]]; do ARR[$i]="${1:$i:$((${#1}-$((${#1}-1))))}"; echo ${ARR[$i]}; let i=$i+1; done }
User@term$ func "This is a string"
T
h
i
s

i
s

a

s
t
r
i
n
g
Remember that 'func this is a string' and 'func "This is a string"' are two different things.

: )
 
Old 10-25-2010, 09:48 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@Gussy - I am not sure what you mean? You created a function and then ran it with a string ... how is that helpful to this thread?

Also is there a reason for the convoluted length expression?
Code:
ARR[$i]="${1:$i:$((${#1}-$((${#1}-1))))}"

# As the length of the input is uniform and not adjusted anywhere else in the code so this is always

ARR[$i]="${1:$i:1}"
 
Old 10-26-2010, 02:51 PM   #15
Gussy
LQ Newbie
 
Registered: Sep 2006
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
@Gussy - I am not sure what you mean? You created a function and then ran it with a string ... how is that helpful to this thread?

Also is there a reason for the convoluted length expression?
Code:
ARR[$i]="${1:$i:$((${#1}-$((${#1}-1))))}"

# As the length of the input is uniform and not adjusted anywhere else in the code so this is always

ARR[$i]="${1:$i:1}"
Opps -- yes; disregard my solution.

I'm very used to seeing people having problems with breaking up strings into characters.
 
  


Reply



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
[SOLVED] Find a substring/member in a string/array using bash someshpr Programming 4 10-14-2010 02:17 AM
bash string to array patolfo Programming 16 05-17-2010 10:12 AM
Programing Assistance - Splitting a string stuaz Programming 6 07-13-2009 09:10 AM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 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