如何在粒度级别使用shell脚本读取CSV文件?

时间:2014-06-07 13:11:20

标签: shell csv

我的示例CSV文件是:

test1,a=1,b=2  
test2,c=3,d=4

我可以使用

获取字段test1 a = 1 b = 2
while read f1 f2 f3  
do
    echo $f1  
    echo $f2  
    echo $f3  
done < filename.csv  

但我希望单独阅读a=1并将a保存在param1中,将1保存在param2中。我想为所有的f2&amp; f3一个接一个。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以将IFS设置为逗号或等号,如下所示:

while IFS=",=" read a b c d e
do 
   echo $a
   echo $b
   echo $c
   echo $d
   echo $e
done < file

<强>输出

test1
a 
1 
b
2
test2
c
3 
d
4