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)

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

8 Comments

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")'
I've updated my answer to skip first line of your file.
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
Sorry, I can't reproduce this problem. Try this: mapfile -s 1 -t array < <(cut -d, -f 1 data.csv); declare -p array
Have a look! Racent bash do offer loadable builtins wht a CSV parser! I'v posted a demo with mulitline fileds in CSV
|

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.