0

This is the first time I try the linux programming... I have a csv file made like this

nothing; V;V;V;F;V;F;V;V;
nothing; V;V;V;F;V;F;V;V;

the columns are fixed but the rows can be increased in future. I want to read the value of each row and save it into a variable, which I need to investigate for future operations. Right now the problem is how to open and store into a variable the value of each cell of the csv.
I've tried with the command

Folder_path=file.csv
For /f "skip=1" in ($Folder_path) 

but it gives me an error.
Furthermore I think a while loop should be better than the for but honestly I'ven't understood how to construct it.

2
  • Please provide enough code so others can better understand or reproduce the problem. Commented Jun 1, 2022 at 0:13
  • as I've written I have a CSV done as you can read before, and from that I want to have in a variable the value "V" the value "F" the value "nothing". how can I do? Commented Jun 1, 2022 at 7:01

1 Answer 1

1
c=1
while read row
do
    echo "row $c:  $row"
    IFS='; ' read -r -a cols_arr <<< "$row"
    #declare -p cols_arr
    for((i=0; i<${#cols_arr[@]}; i++))
    do
        echo "row $c: --- column $(($i+1)): variable is \${cols_arr[$i]} has value ${cols_arr[$i]}"
    done
    c=$((c+1))
done < csvfile


row 1:  nothing; V;V;V;F;V;F;V;V; nothing; V;V;V;F;V;F;V;V;
row 1: --- column 1: variable is ${cols_arr[0]} has value nothing
row 1: --- column 2: variable is ${cols_arr[1]} has value V
row 1: --- column 3: variable is ${cols_arr[2]} has value V
row 1: --- column 4: variable is ${cols_arr[3]} has value V
row 1: --- column 5: variable is ${cols_arr[4]} has value F
row 1: --- column 6: variable is ${cols_arr[5]} has value V
row 1: --- column 7: variable is ${cols_arr[6]} has value F
row 1: --- column 8: variable is ${cols_arr[7]} has value V
row 1: --- column 9: variable is ${cols_arr[8]} has value V
row 1: --- column 10: variable is ${cols_arr[9]} has value nothing
row 1: --- column 11: variable is ${cols_arr[10]} has value V
row 1: --- column 12: variable is ${cols_arr[11]} has value V
row 1: --- column 13: variable is ${cols_arr[12]} has value V
row 1: --- column 14: variable is ${cols_arr[13]} has value F
row 1: --- column 15: variable is ${cols_arr[14]} has value V
row 1: --- column 16: variable is ${cols_arr[15]} has value F
row 1: --- column 17: variable is ${cols_arr[16]} has value V
row 1: --- column 18: variable is ${cols_arr[17]} has value V
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.