I am looking for a quick/easy way to split a string in ksh.

It has varied number delimeters (spaces) between each item.

example:

value1 value2                  value3

Any suggestions/advice?

link|improve this question
feedback

2 Answers

up vote 3 down vote accepted

Using a for loop with the input string will split on whitespace.

LIST="value1  value2 value3"
for x in $LIST ; do
    echo $x
done

Yields

value1
value2
value3

or

LIST="value1  value2 value3"
set -A STRING "$LIST"
for x in $STRING ; do
    echo $x
done

Yields

value1
value2
value3
link|improve this answer
Quick, easy, painless. Thanks for the cogent answer. – user9398 Sep 22 '09 at 14:02
feedback

You can use an array.

LIST="value1  value2 value3"
set -A values $LIST
echo ${values[0]}

value1
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.