1

次のような文字列を含むファイルがあります(任意の文字列の単語数はランダムです):

coge las hojas y las quemas todas en el fuego   k Wo x e l a s Wo x a s i l a s k We m a s t Wo D a s e n e l f w We G o 
la liga de paz se reunió para tratar el tema    l a l Wi G a d e p Wa T s e rr e w n j Wo p a r a t r a t Wa r e l t We m a
el bebé se mete el pie dentro de la boca    e l b e B We s e m We t e e l p j We d We n t r o d e l a b Wo k a
hoy en día el pollo es un plato común   Wo j e n d Wi a e l p Wo L o We s Wu n p l Wa t o k o m Wu n

文字列を単語で区切りたい。たとえば、最初の文から10個の変数v1、v2、.. v10を取得して、次のようにします。

v1="coge"
v2="las"
...
v10="fuego"

よろしくお願いします!!!

4

1 に答える 1

1

3つ以上のスペースで単語が行の残りの部分から分離されていると仮定すると、次のようになります。

while IFS= read -r line; do
    read -ra words <<< ${line%%   *}

    # do whatever you need with the words array here, for example
    for (( i=0; i<${#words[@]}; i++ )); do
        printf "%d - %s\n" $i "${words[i]}"
    done
done < filename

タブ文字を使用するには:

while IFS=$'\t' read -r words phones; do
    read -ra words_ary <<< $words

    # do whatever you need with the words array here, for example
    for (( i=0; i<${#words_ary[@]}; i++ )); do
        printf "%d - %s\n" $i "${words_ary[i]}"
    done
done < filename
于 2013-01-24T21:38:48.253 に答える