Voglio dividere la stringa e costruire l'array. Ho provato il seguente codice:Divisione di una stringa nell'array
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
Potete per favore avvisare?
Ho trovato la soluzione che è sulle seguenti linee: var = 'word1 # word2 | parola3/word4 | word5.word6 | word7_word8 | word9 word10 | word11 | word12 ' OIFS = $ IFS; IFS = '|' set -A arr $ var IFS = $ OIFS –
puoi rimuovere il ciclo for effettuando una modifica come sotto awk 'BEGIN {FS = ":"} {per (i = 1; i <= NF; i ++) print $ i}. Basta tenerlo come awk 'BEGIN {RS = ":"} {print}' – Vijay
bash sul mio sistema (4.0.33 (5) -release) non ha un'opzione -A per 'set' . Quale versione stai correndo? – outis