简体   繁体   中英

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. 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]" . So put the result of tail into an array VALUES and then be able to pick what values I want to display.

With mapfile which is a bash4+ feature

#!/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.

#!/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]}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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