Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

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
share|improve this answer
    
Quick, easy, painless. Thanks for the cogent answer. –  user9398 Sep 22 '09 at 14:02
add comment

You can use an array.

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

value1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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