Skip to content
Advertisement

Get specific column from CSV in Bash script

I have a csv with nearly a hundred of column.

I am writing a bash script to get only few of those column.

CSV:

JavaScript

I did some research and found this:

JavaScript

But it requires to put ALL the column (92 in my case…) I can’t select want I want. So I try another solution:

JavaScript

It cannot work because of the size of the $line. I get the error name too long or something like that. (and it does not look really optimized but anyway) I guess I am close to it. Any help?

Advertisement

Answer

You can use just cut:

JavaScript

Note this only works if the comma is never escaped or quoted in the CSV, and no value can contain a newline.

To iterate over the lines and populate variables, you can do

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement