Ich möchte die Zeichenfolge aufteilen und das Array erstellen. Ich habe den folgenden Code ausprobiert:Zeichenfolge in Array aufteilen
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
Können Sie bitte beraten?
fand ich die Lösung, die auf den folgenden Zeilen ist: var = 'word1 # word2 | word3/Word4 | word5.word6 | word7_word8 | word9 word10 | word11 | word12 ' OIFS = $ IFS; IFS = '|' set -Ar arr $ var IFS = $ OIFS –
Sie können die for-Schleife entfernen, indem Sie eine Änderung wie unten vornehmen awk 'BEGIN {FS = ":"} {für (i = 1; i <= NF; i ++) print $ i} .behalte es als awk 'BEGIN {RS = ":"} {print}' – Vijay
bash auf meinem System (4.0.33 (5) -Release) hat keine -A Option für 'set' . Welche Version laufen Sie? – outis