0

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.

treetop
  • 165
  • 1
  • 13
  • I don't understand what you are trying to do ? Do you want to keep all you arrays ? – Fabich May 10 '16 at 15:49
  • If you want to echo only one line you can create a counter before the while, increment it at each loop and add a test before the echo – Fabich May 10 '16 at 15:54
  • I've edited my question to try and clarify lord of dark, that sounds like the kind of thing I'm looking for but I am unsure of the code, sorry this is new to me how do I create a counter? – treetop May 10 '16 at 16:04

1 Answers1

0

My answer is just guessing, but it maybe can help you. Maybe clarify your question that so I can address my answer better.

#!/bin/bash
input="test.csv"

# it prints specyfic argument of array: array guide http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html
getLine()
{
    local lineNo="$1"
    echo "${inputLines[$lineNo]}"
}

# gets specyfic block of line csv's file
getBlock()
{
    local lineNo="$1"
    local blockNo="$2"

    # split string into array by read function
    # look at: http://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash
    IFS=',' read -r -a block <<< "$(getLine $lineNo)"
    echo "${block[$blockNo]}"
}

getElement() 
{
    local lineNo="$1"
    local blockNo="$2"
    local elementNo="$3"

    read -r -a element <<< "$(getBlock $lineNo $blockNo)"
    echo "${element[$elementNo]}"
}

# read file into an array
# based on nhed response: http://stackoverflow.com/questions/11393817/bash-read-lines-in-file-into-an-array
IFS=$'\r\n' GLOBIGNORE='*' command eval 'inputLines=($(cat $input))'
echo "Loaded ${#inputLines[@]} lines."

getLine 0         # print line of csv
getBlock 0 1      # print block of csv
getElement 0 1 2  # print element of csv
abrzozowski
  • 389
  • 1
  • 3
  • 12
  • Ok, thanks i'll give it a go and get back to you :) could you briefly explain what each bit does so I can get my head around what's going on? – treetop May 11 '16 at 11:05
  • Ok, just had a look, it does 80% of what I would like it to do so thanks for that, one thing I would like to be able to do is call up the full line also, so have an option to see the whole csv line. I know I would need to set a 3rd option for this with a /n delimiter but unsure how to place it in this script. abbie – treetop May 11 '16 at 14:05
  • I updated my answer , i hope this is what you expected. – abrzozowski May 12 '16 at 09:38