I am looking for a quick/easy way to split a string in ksh.
It has varied number delimiters (spaces) between each item.
Example:
value1 value2 value3
Any suggestions/advice?
I am looking for a quick/easy way to split a string in ksh.
It has varied number delimiters (spaces) between each item.
Example:
value1 value2 value3
Any suggestions/advice?
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
You can use an array.
LIST="value1 value2 value3"
set -A values $LIST
echo ${values[0]}
value1
Piping through sed and optionally grep works too
LIST="value1 value2 value3" ;
echo "$LIST" | sed 's/[[:space:]]/\n/g' | grep .
You can drop the grep if you also drop the quotes which removes multiple spaces though other formatting may be effected.
LIST="value1 value2 value3" ;
echo $LIST | sed 's/[[:space:]]/\n/g'