How to parse CSV file for long row in Bash?

I have IDs in the first column of data.csv with headers. I want to skip the header and store column 1 values in the variable ids as 102 103 104 .... Pseudocode in the line ids.append($col1) where I want to append the current row value to the end of the line with a space

# http://stackoverflow.com/a/4286841/54964
while IFS=, read col1
do
    ids.append($col1) # Pseudocode
done < data.csv

data.csv

102
103
104

Expected output

ids=( 102 103 104 )

OS: Debian 8.5
Bash: 4.3.30(1)

Upvotes: 0

Views: 404

Answers (1)

Cyrus
Cyrus

Reputation: 88756

With GNU bash and GNU tail:

#!/bin/bash

array=()
while IFS=, read -r col1 coln
do
    array+=("$col1") # append $col1 to array array
done < <(tail -n +2 data.csv)

declare -p array

Upvotes: 2

Related Questions