如何拥有庆典解析一个CSV文件? [英] How to have bash parse a CSV file?

查看:109
本文介绍了如何拥有庆典解析一个CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作很长的bash脚本。我想读从CSV文件细胞进入bash的变量。我可以解析行和第一列,但不排除任何其他列。这里是我的code迄今:

I'm working on a long bash script. I want to read cells from a CSV file into bash variables. I can parse lines and the first column, but not any other column. Here's my code so far:


  cat myfile.csv|while read line
  do
    read -d, col1 col2 < <(echo $line)
    echo "I got:$col1|$col2"
  done

这只是打印的第一列。作为一个额外的测试,我试过如下:

It's only printing the first column. As an additional test, I tried the following:

读-d,X Y&LT; ≤(回声A,B)

和$ y是空的。所以我尝试:

And $y is empty. So I tried:

读取x和y&LT; ≤(回声B)

和$ y是 B 。为什么呢?

And $y is b. Why?

推荐答案

您需要使用 IFS 而不是 -d

while IFS=, read col1 col2
do
    echo "I got:$col1|$col2"
done < myfile.csv

这篇关于如何拥有庆典解析一个CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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