How to parse CSV file for long row in Bash?
Asked Answered
C

1

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

# https://mcmap.net/q/41008/-how-to-parse-a-csv-file-in-bash
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)

Curvature answered 30/10, 2016 at 16:49 Comment(0)
E
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
Ermines answered 30/10, 2016 at 16:59 Comment(8)
I use this here to show you content of array array.Ermines
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")'Ermines
I've updated my answer to skip first line of your file.Ermines
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.csvErmines
Sorry, I can't reproduce this problem. Try this: mapfile -s 1 -t array < <(cut -d, -f 1 data.csv); declare -p arrayErmines
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.Mam
Okay. mapfile is a bash builtin command. For more information see help mapfile.Ermines
Have a look! Racent bash do offer loadable builtins wht a CSV parser! I'v posted a demo with mulitline fileds in CSVZeuxis

© 2022 - 2024 — McMap. All rights reserved.