如何在Bash中解析CSV文件以获得长行? [英] How to parse CSV file for long row in Bash?

查看:49
本文介绍了如何在Bash中解析CSV文件以获得长行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在data.csv第一栏中有标头的ID.我想跳过标题并将列1的值存储在变量 ids 中,作为 102103104 ... . ids.append($ col1)行中的伪代码,我想在其中将当前行值附加到行尾并带有空格

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

预期产量

ids=( 102 103 104 )

操作系统:Debian 8.5
重击:4.3.30(1)

OS: Debian 8.5
Bash: 4.3.30(1)

推荐答案

具有GNU bash和GNU尾部:

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

这篇关于如何在Bash中解析CSV文件以获得长行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆