如何从 BASH 中的数组中提取特定元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15685736/
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
How to extract a particular element from an array in BASH?
提问by minerals
Here is how I create my bash array:
这是我创建 bash 数组的方法:
while read line
do
myarr[$index]=$line
index=$(($index+1))
done < lines.txt
The file "lines.txt" constists of the following strings
文件“lines.txt”由以下字符串组成
hello big world!
how are you
where am I
After creation of ${myarr[@]}
I can easily access every element (line) in this array issuing
创建后,${myarr[@]}
我可以轻松访问此数组中的每个元素(行)发出
echo ${myarr[2]}
But what if I want to extract just world!
? Is it possible to extract world!
from 0 element of the myarr
? What's most important, is it possible to extract any last word from the myarr
element?
但是如果我只想提取world!
呢?是否可以world!
从 0 元素中提取myarr
?最重要的是,是否可以从myarr
元素中提取任何最后一个单词?
I know that in python you can do myarr[0][3]
and that will do the trick, how about bash?
我知道在 python 中你可以做到myarr[0][3]
并且可以解决问题,那么 bash 怎么样?
回答by Steven Penny
This is one of many ways
这是许多方法之一
set ${myarr[2]}
echo
回答by Gordon Davisson
You can extract words from a string (which is what the array elements are) using modifiers in the variable expansion: #
(remove prefix), ##
(remove prefix, greedy), %
(remove suffix), and %%
(remove suffix, greedy).
您可以使用变量扩展中的修饰符从字符串(数组元素是什么)中提取单词:(#
删除前缀)、##
(删除前缀,贪婪)、%
(删除后缀)和%%
(删除后缀,贪婪)。
$ myarr=('hello big world!' 'how are you' 'where am I')
$ echo "${myarr[0]}" # Entire first element of the array
hello big world!
$ echo "${myarr[0]##* }" # To get the last word, remove prefix through the last space
world!
$ echo "${myarr[0]%% *}" # To get the first word, remove suffix starting with the first space
hello
$ tmp="${myarr[0]#* }" # The second word is harder; first remove through the first space...
$ echo "${tmp%% *}" # ...then get the first word of what remains
big
$ tmp="${myarr[0]#* * }" # The third word (which might not be the last)? remove through the second space...
$ echo "${tmp%% *}" # ...then the first word again
world!
As you can see, you can get fairly fancy here, but at some point @chepner's suggestion of turning it into an array gets much easier. Also, the formulae I suggest for extracting the second etc word are a bit fragile: if you use my formula to extract the third word of a string that only has two words, the first trim will fail, and it'll wind up printing the first(!) word instead of a blank. Also, if you have two spaces in a row, this will treat it as a zero-length word with a space on each side of it...
正如您所看到的,您可以在这里变得相当花哨,但在某些时候@chepner 将其转换为数组的建议变得更加容易。此外,我建议的提取第二个 etc 词的公式有点脆弱:如果您使用我的公式提取只有两个词的字符串的第三个词,则第一个修剪将失败,并且最终会打印第一个(!)词而不是空白。此外,如果您连续有两个空格,这会将其视为零长度单词,每侧都有一个空格...
BTW, when building the array I consider it a bit cleaner to use +=(newelement)
rather than keeping track of the array index explicitly:
顺便说一句,在构建数组时,我认为它使用起来更简洁,+=(newelement)
而不是明确跟踪数组索引:
myarr=()
while read line, do
myarr+=("$line")
done < lines.txt
回答by Michael P.
Similar to stephen-penny'sanswer, but without overwriting shell/function positional parameters.
类似于stephen-penny 的回答,但没有覆盖 shell/函数位置参数。
a=(${myarr[2]})
echo ${a[3]}
回答by Amirouche Zeggagh
to print specific element from array using the index :
使用索引打印数组中的特定元素:
echo ${my_array[2]}
to print all elements from array you do :
要打印您所做的数组中的所有元素:
for i in "${my_array[@]}"
do
echo $i
done