Quiero dividir la cadena y construir la matriz. Probé el siguiente código:División de cadena en matriz
myString="first column:second column:third column"
set -A myArray `echo $myString | awk 'BEGIN{FS=":"}{for (i=1; i<=NF; i++) print $i}'`
# Following is just to make sure that array is constructed properly
i=0
while [ $i -lt ${#myArray[@]} ]
do
echo "Element $i:${myArray[$i]}"
((i=i+1))
done
exit 0
It produces the following result:
Element 0:first
Element 1:column
Element 2:second
Element 3:column
Element 4:third
Element 5:column
This is not what I want it to be. When I construct the array, I want that array to contain only three elements.
Element 0:first column
Element 1:second column
Element 2:third column
¿Puede por favor asesorar?
he encontrado la solución que se encuentra en las siguientes líneas: var = '# palabra1 palabra2 | word3/WORD4 | word5.word6 | word7_word8 | WORD9 word10 | word11 | word12 ' OIFS = $ IFS; IFS = '|' establecer -A arr $ var IFS = $ OIFS –
puede eliminar el bucle for haciendo un cambio como el siguiente awk 'BEGIN {FS = ":"} {for (i = 1; i <= NF; i ++) print $ i} .just keep it as awk 'BEGIN {RS = ":"} {print}' – Vijay
bash en mi sistema (4.0.33 (5) -release) no tiene una -A opción para 'set' . ¿Qué versión está usando? – outis