LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash string splitting into array (https://www.linuxquestions.org/questions/programming-9/bash-string-splitting-into-array-838126/)

praveenhm 10-14-2010 05:54 PM

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

swingliner 10-14-2010 06:01 PM

http://www.unix.com/shell-programmin...tion-bash.html

ghostdog74 10-14-2010 06:43 PM

Code:

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


praveenhm 10-15-2010 12:04 AM

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

praveenhm 10-15-2010 12:16 AM

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

konsolebox 10-15-2010 01:47 AM

i'd prefer to use read for that:
Code:

IFS=...
read -a VAR <<< "$STRING"

assuming:
Code:

[[ BASH_VERSINFO -ge 3 ]]

catkin 10-15-2010 09:13 AM

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

grail 10-15-2010 11:00 AM

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

praveenhm 10-15-2010 01:46 PM

Thanks Grail, your solution looks idle,
Thanks catkin and Konsolebox giving soulution.

Pen

grail 10-16-2010 03:46 AM

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.

catkin 10-16-2010 08:33 AM

Quote:

Originally Posted by grail (Post 4128641)
This way the IFS change is localized to the action and no need to check or change back

Nice improvement grail :)

konsolebox 10-19-2010 06:39 AM

Nice one grail. :)

Now I no longer have to use a wrapper function. Or maybe now just optional.

Gussy 10-25-2010 08:01 AM

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.

: )

grail 10-25-2010 09:48 AM

@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}"


Gussy 10-26-2010 02:51 PM

Quote:

Originally Posted by grail (Post 4138688)
@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.


All times are GMT -5. The time now is 07:15 PM.