I have a csv with data such as:
L'Oreal, Apricot and Peach Conditioner, Sale
Sure, Camomile Deodorant Spray, Standard
Brut, Classic Aftershave, Sale
I've written some code to turn each field into a variable, and also to create an array out of each word in field 2 (f2):
#!/bin/bash
input="/home/abbie/Documents/Scripts/test.csv"
#read the csv and creates a variable for each field
while IFS=',' read -r f1 f2 f3 f4
do
#Create an array out of each word in field 2 which is the description field
read -a arr <<<$f2
#print the specified array- the first word is ${arr[0]}
echo ${arr[0]}
done < "$input"
My problem is now, because the file contains multiple lines I need some way of naming each array according to the line of data it was created from. At the moment when I echo ${arr[0]}
it prints the first word from f2 of every line, when I would like to be able to be able to call up a specific line.
EDIT: To clarify (sorry I'm a noob and didn't explain very well) I would like to somehow change my code so that rather than creating an array for each separate word in f2 throughout the file, I want to create an array for each word in f2 on a line by line basis and somehow be able to display the line of the csv it came from.