列数未知的csv文件的Bash处理 [英] Bash processing of csv file with unknown number of columns
问题描述
我正在尝试使用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屋!