简体   繁体   English

Bash将字符串解析为浮点数数组

[英]Bash-parsing string into an array of floating point numbers

I wish to create an array of floating point numbers (called b) in BASH, the contents of the array is given by parsing the following variable adse : 我希望在BASH中创建一个浮点数数组(称为b),该数组的内容通过解析以下变量adse

echo $adse
16.92 18.29 19.18 20.87 2.78 2.88 2.77 2.83 2.80 2.78 2.73 2.73 2.75 2.93 2.91 2.93 2.77 4.64 2.67 3.01 6.78

so that b[1]=16.92; 这样b [1] = 16.92; b[2]=18.29..... b [2] = 18.29 .....

How can I achieve this? 我该如何实现?

With

b=($adse)

you get a bash array b . 你得到一个bash数组b Individual arguments can be accessed with ${b[index]} . 可以使用${b[index]}访问单个参数。 Indices are zero-based, so the first element is ${b[0]} . 索引从零开始,因此第一个元素是${b[0]}

Be aware that you will find it difficult to do anything with these values in bash, though. 请注意,尽管如此,使用bash中的这些值将很难执行任何操作。 It might be a good idea to use a scripting language that has support for floating point calculations, such as Perl or Python. 使用支持浮点计算的脚本语言(例如Perl或Python)可能是一个好主意。

For a more in-depth discussion of bash arrays, see this link . 有关bash数组的更深入讨论,请参见此链接

The canonical solution is: 规范的解决方案是:

read -r -d '' -a b <<<"$adse"

Unlike solutions which rely on default word-splitting of an unquoted expansion of $adse , the read built-in will not produce unwanted expansions of glob characters. 与依靠$adse的不带引号的扩展名的默认单词拆分的解决方案不同,内置的read不会产生不需要的glob字符扩展。

If you want to split the variable on some character other than whitespace, you can set IFS locally for the read : 如果要在空格以外的其他字符上拆分变量,则可以在本地设置IFSread

IFS=: read -r -d '' -a b <<<"$adse"

That will not split on newlines. 那不会在换行符上分开。 If you wanted to split on colon or newline, you could use IFS=$':\\n' . 如果要在冒号或换行符上分割,则可以使用IFS=$':\\n'

Both of the above will set b[0] to the first element, not b[1] . 以上两个都将b[0]设置为第一个元素,而不是b[1] If you wanted to start at b[1] , you could prepend 0 or some such to the input to read , and then unset "b[0]" afterwards. 如果要从b[1] ,则可以在输入中添加0或类似的值以read ,然后再unset "b[0]"

help read to get an explanation of the options. help read以获取有关选项的说明。 Briefly, -r avoids interpretation of backslash escape sequences; 简而言之, -r避免反斜杠转义序列的解释; -d '' causes the read to terminate at the end of input instead of the end of a line, and -ab causes the result to be placed in the array b . -d ''使read终止于输入的末尾而不是行的末尾,并且-ab使结果放置在数组b

Try with: 尝试:

b=($(echo $adse))

But it begins with index 0, like: 但是它以索引0开头,例如:

echo ${b[0]}

that yields: 产生:

16.92

Although there is an easy solution to achieve what you want, I believe the following is also useful if, say, you have a string whose delimiters are not whitespace: 尽管有一个简单的解决方案可以实现所需的目标,但是我相信如果您有一个其分隔符不是空格的字符串,则以下内容也很有用:

b=()
adse="16.92 18.29 19.18 20.87 2.78 2.88 2.77 2.83 2.80 2.78 2.73 2.73 2.75 2.93 2.91 2.93 2.77 4.64 2.67 3.01 6.78"

b=(${adse// / })

For example, if you had a string like this: 例如,如果您有这样的字符串:

adse="16.92:18.29...etc"

You would have to change b=(${adse// / }) to: 您必须将b=(${adse// / })更改为:

b=(${adse//:/ })

However, for your particular case, all that is needed to parse the string into an array is already stated by Birei below. 但是,对于您的特定情况,下面的Birei已经说明了将字符串解析为数组所需的所有操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM