[英]I want to parse a .csv file and take the last line and store it in an array to then display some select values on the terminal
so I am currently displaying the last line of a csv file using tail -n 1 file1.csv.所以我目前使用 tail -n 1 file1.csv 显示 csv 文件的最后一行。 That results in the values being displayed in one line just separated by commas.
这导致值显示在一行中,仅用逗号分隔。 I want to be able to display a few select values by doing this
echo "Value 3: $VALUES[2]"
.我希望能够通过执行此
echo "Value 3: $VALUES[2]"
来显示一些 select 值。 So put the result of tail into an array VALUES and then be able to pick what values I want to display.因此,将 tail 的结果放入数组 VALUES 中,然后能够选择我想要显示的值。
With mapfile
which is a bash4+ feature使用 bash4+ 功能的
mapfile
#!/usr/bin/env bash
mapfile -td, VALUES < <(tail -n 1 file1.csv)
for i in "${!VAlUES[@]}"; do
echo "$i" "${VALUES[$i]}"
done
If mapfile
is not available.如果
mapfile
文件不可用。
#!/usr/bin/env bash
IFS=, read -ra VALUES < <(tail -n 1 file1.csv)
for i in "${!VAlUES[@]}"; do
echo "$i" "${VALUES[$i]}"
done
I want to be able to display a few select values by doing this echo "Value 3: $VALUES[2]"
我希望能够通过执行此 echo "Value 3: $VALUES[2]" 来显示一些 select 值
echo "Value 3: ${VALUES[2]}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.