简体   繁体   English

我想解析 a.csv 文件并取最后一行并将其存储在一个数组中,然后在终端上显示一些 select 值

[英]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.

相关问题 我想读取csv文件并将其解析为数组但总是失败 - i want to read csv file and parse it to array but always fail 一次读取一行 CSV 文件,然后在循环内将每一行解析为 Class 字段,然后将该 Class Z4970317944151AC3524B 存储到数组中 - Read a CSV file one line at a time and then within the loop parse each line into the Class fields and then store that Class Object into an array 我想将 4 个字符串作为输入并显示到另一个数组中 - I want to take 4 strings as an input and display into another array 解析包含php数组新行的CSV文件 - Parse CSV file which contains a new line to php array 如何将 CSV 文件中的 int 值存储在单独的二维数组元素中 - How do I store int values from CSV file in separate two-dimensional array elements 我希望能够存储和显示来自数组和本地存储内部的数据? - i want to be able to store and display data from an array and inside localstorage? 如何将 csv 值从文件存储到 Python 中的 numpy 数组? - How to store csv values form a file to a numpy array in Python? 在数组中显示csv文件 - Display csv file in array 我要求用户输入他的(int)ID,然后我想获取 ID 并将其存储在数组中以继续 - I asked the user to enter his (int)ID and then I want to take the ID and store it in an array to continue 我有一个输出,我想以最多五个值的数组显示它 - I have an output and I want to display it in array upto five values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM