0

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)

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

1 Answers1

2

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
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • I use this here to show you content of array array. – Cyrus Oct 30 '16 at 17:24
  • 1
    If I replace `,` by `:` and `data.csv` by `/etc/passwd` it returns all users from my Linux system: `declare -a array='([0]="root" [1]="daemon" [2]="bin" [3]="sys" [4]="sync" [5]="games" [6]="man" [7]="lp" [8]="mail" [9]="news" [10]="uucp" [11]="proxy" [12]="www-data" [13]="backup" [14]="list" [15]="irc" [16]="gnats" [17]="nobody" [18]="libuuid" [19]="syslog" [20]="messagebus" [21]="usbmux" [22]="avahi-autoipd" [23]="avahi" [24]="kernoops" [25]="pulse" [26]="rtkit" [27]="hplip" [28]="kdm" [29]="saned" [30]="bar" [31]="sshd" [32]="cyrus" [33]="foo" [34]="dnsmasq")'` – Cyrus Oct 30 '16 at 17:28
  • 1
    I've updated my answer to skip first line of your file. – Cyrus Oct 30 '16 at 17:47
  • 1
    `col1` should contain content of first column and `coln` should contain content of all remaining columns (here 2 and 3). I suggest to check your file for special characters: `cat -v data.csv` – Cyrus Oct 30 '16 at 18:04
  • 1
    Sorry, I can't reproduce this problem. Try this: `mapfile -s 1 -t array < <(cut -d, -f 1 data.csv); declare -p array` – Cyrus Oct 30 '16 at 18:16
  • 1
    It works! Sorry, it was my typo in in the variable name. Output is now: `declare -a ids='([0]="100" [1]="101" [2]="102"` which seems to be correct. – Léo Léopold Hertz 준영 Oct 30 '16 at 18:18
  • 1
    Okay. `mapfile` is a bash builtin command. For more information see `help mapfile`. – Cyrus Oct 30 '16 at 18:24
  • 2
    Have a look! Racent [tag:bash] do offer loadable builtins wht a CSV parser! I'v posted a [demo with mulitline fileds in CSV](https://stackoverflow.com/a/69514496/1765658) – F. Hauri - Give Up GitHub Oct 21 '21 at 17:45