How do you read a csv file into a two dimensional array in BASH? The script needs to be dynamic enough where it can take csv files with variable number of rows and columns.
For example, if I have a csv file that looks like
AVERAGE STDEV MAX
17 18 19
or
AVERAGE STDEV MAX MIN
17 18 19 1
One way to simulate a two-dimensional array is to keep the rows as strings in a one-dimensional array and unpack them at each iteration. You will have to choose a suitable delimiter that doesn’t appear in the data. Since you mention CSV, I’ll use a comma, but this won’t be smart enough to handle data like this with embedded commas:
Here’s a simple example of iterating over the values in a simulated two-dimensional array:
Making changes to the contents of the array is possible:
As you can see, it gets complicated fast and there are lots of gotchas which I haven’t mentioned. Use Python.