BASH - 如何从CSV文件中的列提取数据,并把它放在一个数组? [英] BASH - How to extract data from a column in CSV file and put it in an array?

查看:1069
本文介绍了BASH - 如何从CSV文件中的列提取数据,并把它放在一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习到脚本猛砸。

我有一个CSV 5列和22列的文件。我感兴趣的是从第二列取数据,并把它变成一个数组。

I have a CSV file with 5 columns and 22 rows. I am interested in taking the data from the second column and put it into an array.

我要的是,第一个名字是数组[0] 第二个数组[1] 等。

What I want is that the first name be in array[0], the second in array[1] and so on.

Bash脚本:

#!/bin/sh
IFS=","
eCollection=( $(cut -d ',' -f2 MyAssignment.csv ) )
printf "%s\n" "${eCollection[0]}"

该CSV看起来是这样的。没有与头都不行。

The CSV looks like this. There is no line with headers.

Vl18xx 号码列是我想分割成一个数组的东西。

The column with the Vl18xx numbers is what I want to split into an array.

John,Vl1805,VRFname,10.9.48.64/28,10.9.48.78 
John,Vl1806,VRFname,10.9.48.80/28,10.9.48.94
John,Vl1807,VRFname,10.9.48.96/28,10.9.48.110 
John,Vl1808,VRFname,10.9.48.112/28,10.9.48.126
John,Vl1809,VRFname,167.107.152.32/28,167.107.152.46

在bash脚本没有放置第2列到数组,我究竟做错了什么?

The bash script is not placing the 2nd column into the array, what am I doing wrong?

推荐答案

删除 IFS =,分配从而让空间的默认IFS值,制表符,换行符适用

Remove the IFS="," assignment thereby letting the default IFS value of space, tab, newline apply

#!/bin/bash

eCollection=( $(cut -d ',' -f2 MyAssignment.csv ) )
printf "%s\n" "${eCollection[0]}"

这篇关于BASH - 如何从CSV文件中的列提取数据,并把它放在一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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