bash linux shell 脚本:拆分字符串,将它们放在一个数组中,然后循环遍历它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1406966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:30:56  来源:igfitidea点击:

linux shell script: split string, put them in an array then loop through them

bash

提问by Funky Dude

Possible Duplicate:
Split string based on delimiter in Bash?

可能的重复:
基于 Bash 中的分隔符拆分字符串?

In a bash script how do I split string with a separator like ;and loop through the resulting array?

在 bash 脚本中,如何使用分隔符分割字符串;并循环遍历结果数组?

回答by user132447

You can probably skip the step of explicitly creating an array...

您可能可以跳过显式创建数组的步骤...

One trick that I like to use is to set the inter-field separator (IFS) to the delimiter character. This is especially handy for iterating through the space or return delimited results from the stdout of any of a number of unix commands.

我喜欢使用的一个技巧是将字段间分隔符 (IFS) 设置为分隔符。这对于遍历空间或从许多 unix 命令中的任何一个的 stdout 返回分隔结果特别方便。

Below is an example using semicolons (as you had mentioned in your question):

以下是使用分号的示例(正如您在问题中提到的):

export IFS=";"
sentence="one;two;three"
for word in $sentence; do
  echo "$word"
done

Note: in regular Bourne-shell scripting setting and exporting the IFS would occur on two separate lines (IFS='x'; export IFS;).

注意:在常规的 Bourne-shell 脚本设置和导出 IFS 将发生在两个单独的行(IFS='x';导出 IFS;)。

回答by NVRAM

If you don't wish to mess with IFS (perhaps for the code within the loop) this might help.

如果您不想弄乱 IFS(可能是循环中的代码),这可能会有所帮助。

If know that your string will not have whitespace, you can substitute the ';' with a space and use the for/inconstruct:

如果知道您的字符串不会有空格,您可以替换 ' ; ' 带一个空格并使用for/in结构:

#local str
for str in ${STR//;/ } ; do 
   echo "+ \"$str\""
done

But if you mighthave whitespace, then for this approach you will need to use a temp variable to hold the "rest" like this:

但是,如果您可能有空格,那么对于这种方法,您将需要使用一个临时变量来保存“其余部分”,如下所示:

#local str rest
rest=$STR
while [ -n "$rest" ] ; do
   str=${rest%%;*}  # Everything up to the first ';'
   # Trim up to the first ';' -- and handle final case, too.
   [ "$rest" = "${rest/;/}" ] && rest= || rest=${rest#*;}
   echo "+ \"$str\""
done

回答by Paused until further notice.

Here's a variation on ashirazi'sanswer which doesn't rely on $IFS. It does have its own issues which I ouline below.

这是ashirazi答案的一个变体,它不依赖于$IFS. 它确实有自己的问题,我将在下面列出。

sentence="one;two;three"
sentence=${sentence//;/$'\n'}  # change the semicolons to white space
for word in $sentence
do
    echo "$word"
done

Here I've used a newline, but you could use a tab "\t" or a space. However, if any of those characters are in the text it will be split there, too. That's the advantage of $IFS- it can not only enable a separator, but disable the default ones. Just make sure you save its value before you change it - as others have suggested.

这里我使用了换行符,但您可以使用制表符“ \t”或空格。但是,如果这些字符中的任何一个在文本中,它也会在那里拆分。这就是优点$IFS- 它不仅可以启用分隔符,还可以禁用默认分隔符。只要确保在更改它之前保存它的值 - 正如其他人所建议的那样。

回答by kawauso

sentence="one;two;three"
a="${sentence};"
while [ -n "${a}" ]
do
    echo ${a%%;*}
    a=${a#*;}
done

回答by NawaMan

Here is an example code that you may use:

这是您可以使用的示例代码:

$ STR="String;1;2;3"
$ for EACH in `echo "$STR" | grep -o -e "[^;]*"`; do
    echo "Found: \"$EACH\"";
done

grep -o -e "[^;]*" will select anything that is not ';', therefore spliting the string by ';'.

grep -o -e "[^;]*" 将选择任何不是 ';' 的东西,因此用 ';' 分割字符串。

Hope that help.

希望有所帮助。