How to parse CSV file for long row in Bash?

拥有回忆 提交于 2019-12-10 12:19:50

问题


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:


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


来源:https://stackoverflow.com/questions/40331348/how-to-parse-csv-file-for-long-row-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!