列数未知的csv文件的Bash处理 [英] Bash processing of csv file with unknown number of columns

查看:53
本文介绍了列数未知的csv文件的Bash处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bash学习一些文本处理.

I am trying to learn some text processing using bash.

如何制作bash脚本以读取和处理具有第一行作为列标题的未知列数的CSV文件?

How to make a bash script to read and process CSV file with unknown number of columns with first row as column headers?

示例输入:

column1,column2,...,columnn
value11,value12,...,value1n
value21,value22,...,value2n
...
valuem1,valuem2,...,valuemn

输出:

column1: value11
column2: value12
...
columnn: value1n

column1: value21
column2: value22
...
columnn: value2n

...

column1: valuem1
column2: valuem2
...
columnn: valuemn

推荐答案

一种简单的方法是设置 IFS =,并使用 read -a 读入数组:

One simple approach is to set IFS=, and use read -a to read into an array:

#!/bin/bash
IFS=','
read -a headers
while read -a line; do
    for i in "${!line[@]}"; do
        echo "${headers[i]}: ${line[i]}"
    done
done

发生的情况是,第一行被读取到一维数组 $ line 中,并根据 $ IFS 中的字符进行了拆分.在有可用输入的情况下,以相同的方式读取后续行,并且"$ {!line [@]}" 中的指示bash循环遍历数组索引数组值.

What happens is that the first line is read into the one-dimensional array $line, being split according to the character in $IFS. Subsequent lines are read in the same way while there is input available, and the ! in "${!line[@]}" instructs bash to loop over array indices instead of array values.

如果数据使用任何形式的转义方法来包含逗号文字,则此方法将无效.

This will not work if the data use any sort of escaping method to include comma literals.

这篇关于列数未知的csv文件的Bash处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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