String 如何在shell脚本中逐字解析字符串?

String 如何在shell脚本中逐字解析字符串?,string,shell,String,Shell,我有一个包含以下字符串的文件(任何字符串中的字数都是随机的): 我想用单词来分隔字符串。例如,我想从第一句话中得到10个变量v1,v2,…v10,以便: v1="coge" v2="las" ... v10="fuego" 提前感谢您的帮助 假设有3个或更多空格将单词与行的其余部分隔开: while IFS= read -r line; do read -ra words <<< ${line%% *} # do whatever you need wi

我有一个包含以下字符串的文件(任何字符串中的字数都是随机的):

我想用单词来分隔字符串。例如,我想从第一句话中得到10个变量v1,v2,…v10,以便:

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

提前感谢您的帮助

假设有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
而IFS=read-r行;做

read-ra words假设有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
而IFS=read-r行;做

阅读-ra单词您可以使用此[链接][1]找到您的答案:)[1]:第4行的第10个单词应该是“Wo”吗?您确定“单词”何时结束的标准是什么?单词列表通过一个选项卡与该行的电话列表分开。您可以使用此[链接][1]找到您的答案:)[1]:第4行的第10个单词应该是“Wo”吗?您判断“单词”何时结束的标准是什么?单词列表与电话列表之间用一个标签隔开,非常感谢!一个标签将单词与行的其他部分分开。我只想把单词放在电话列表之前。事实上,我想去掉每一行中的电话列表。非常感谢!一个标签将单词与行的其他部分分开。我只想把单词放在电话列表之前。事实上,我想去掉每行电话的列表。
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