How do you read a csv file into a two dimensional array in BASH? The script needs to be dynamic enough where it can take csv files with variable number of rows and columns.
你如何在BASH中将csv文件读入二维数组?该脚本需要足够动态,它可以使csv文件具有可变数量的行和列。
For example, if I have a csv file that looks like
例如,如果我有一个看起来像的csv文件
AVERAGE STDEV MAX
17 18 19
or
要么
AVERAGE STDEV MAX MIN
17 18 19 1
2 个解决方案
#1
4
One way to simulate a two-dimensional array is to keep the rows as strings in a one-dimensional array and unpack them at each iteration. You will have to choose a suitable delimiter that doesn't appear in the data. Since you mention CSV, I'll use a comma, but this won't be smart enough to handle data like this with embedded commas:
模拟二维数组的一种方法是将行保存为一维数组中的字符串,并在每次迭代时解压缩它们。您必须选择一个未出现在数据中的合适分隔符。既然你提到了CSV,我会使用一个逗号,但是这对于使用嵌入式逗号来处理这样的数据是不够智能的:
name, start date, visits, games, balance
"Williamson, Dennis", "January 11, 2007", 12, 42, 17000
Here's a simple example of iterating over the values in a simulated two-dimensional array:
这是一个迭代模拟二维数组中的值的简单示例:
# avg, stddev, max, min
data_array=(
"17,18,19,1"
"12,14,16,2"
"6,8,10,3"
)
saveIFS=$IFS
for row in ${data_array[@]}
do
IFS=","
cols=($row)
IFS=$saveIFS
for col in ${cols[@]}
do
newval=$(do_something $col)
done
done
Making changes to the contents of the array is possible:
可以更改数组的内容:
rowidx=2
colidx=2
IFS=","
cols=(${data_array[rowidx]})
cols[colidx]=$some_value
data_array[rowidx]="${cols[*]}"
IFS=$saveIFS
As you can see, it gets complicated fast and there are lots of gotchas which I haven't mentioned. Use Python.
正如你所看到的那样,它很快变得复杂,并且有很多我没有提到过的问题。使用Python。
#2
0
bash supports only one-dimensional arrays. To see an emulation of 2 dimensions check out twodim.sh in the advanced bash scripting guide:
bash仅支持一维数组。要查看2维的模拟,请查看高级bash脚本编制指南中的twodim.sh:
example 27.17 in
例子27.17 in
http://tldp.org/LDP/abs/html/arrays.html
http://tldp.org/LDP/abs/html/arrays.html
And I agree this does sound like homework.
我同意这听起来像家庭作业。
#1
4
One way to simulate a two-dimensional array is to keep the rows as strings in a one-dimensional array and unpack them at each iteration. You will have to choose a suitable delimiter that doesn't appear in the data. Since you mention CSV, I'll use a comma, but this won't be smart enough to handle data like this with embedded commas:
模拟二维数组的一种方法是将行保存为一维数组中的字符串,并在每次迭代时解压缩它们。您必须选择一个未出现在数据中的合适分隔符。既然你提到了CSV,我会使用一个逗号,但是这对于使用嵌入式逗号来处理这样的数据是不够智能的:
name, start date, visits, games, balance
"Williamson, Dennis", "January 11, 2007", 12, 42, 17000
Here's a simple example of iterating over the values in a simulated two-dimensional array:
这是一个迭代模拟二维数组中的值的简单示例:
# avg, stddev, max, min
data_array=(
"17,18,19,1"
"12,14,16,2"
"6,8,10,3"
)
saveIFS=$IFS
for row in ${data_array[@]}
do
IFS=","
cols=($row)
IFS=$saveIFS
for col in ${cols[@]}
do
newval=$(do_something $col)
done
done
Making changes to the contents of the array is possible:
可以更改数组的内容:
rowidx=2
colidx=2
IFS=","
cols=(${data_array[rowidx]})
cols[colidx]=$some_value
data_array[rowidx]="${cols[*]}"
IFS=$saveIFS
As you can see, it gets complicated fast and there are lots of gotchas which I haven't mentioned. Use Python.
正如你所看到的那样,它很快变得复杂,并且有很多我没有提到过的问题。使用Python。
#2
0
bash supports only one-dimensional arrays. To see an emulation of 2 dimensions check out twodim.sh in the advanced bash scripting guide:
bash仅支持一维数组。要查看2维的模拟,请查看高级bash脚本编制指南中的twodim.sh:
example 27.17 in
例子27.17 in
http://tldp.org/LDP/abs/html/arrays.html
http://tldp.org/LDP/abs/html/arrays.html
And I agree this does sound like homework.
我同意这听起来像家庭作业。