bash 循环遍历 CSV 文件并在读取时创建新的 csv 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13434260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:48:56  来源:igfitidea点击:

Loop through CSV file and create new csv file with while read?

bashtextcsvpattern-matching

提问by Buttle Butkus

I have

我有

while read $field1 $field2 $field3 $field4
do
  $trimmed=$field2 | sed 's/ *$//g'
  echo "$trimmed","$field3" >> new.csv
done < "$FEEDS"/"$DLFILE"

Now the problem is with readI can't make it split fields csv style, can I? See the input csv format below.

现在的问题是read我无法将其拆分为 csv 样式,可以吗?请参阅下面的输入 csv 格式。

I need to get columns 3 and 4 out, stripping the padding from col 2, and I don't need the quotes.

我需要取出第 3 列和第 4 列,从第 2 列中去除填充,并且不需要引号。

Csv format with col numbers: 12 24(")25(,)26(")/27(Field2values) 42(")/43(,)/44(Field3 decimal values) "Field1_constant_value","Field2values ",Field3,Field4

带有列号的 Csv 格式:12 24(")25(,)26(")/27(Field2values) 42(")/43(,)/44(Field3 十进制值) "Field1_constant_value","Field2values ",Field3,场 4

Field1 is constant and irrelevant. Data is quoted, goes from 2-23 inside the quotes. Field2 fixed with from cols 27-41 inside quotes, with the data at the left and padded by spaces on the right. Field3 is a decimal number with 1,2, or 3 digits before the decimal and 2 after, no padding. Starts at col 74. Field4 is a date and I don't much care about it right now.

Field1 是常数且无关紧要。数据被引用,从引号内的 2-23 开始。Field2 用引号内的 cols 27-41 固定,数据在左边,右边用空格填充。Field3 是一个十进制数,小数点前有 1,2 或 3 位数字,小数点后有 2 位,没有填充。从第 74 栏开始。 Field4 是一个日期,我现在不太关心它。

回答by Rubens

Yes, you can use read; all you've got to do is reset the environment variable IFS-- Internal Field Separator --, so that it won't split lines by its current value (default to whitespace), but by your own delimiter.

是的,您可以使用 read;你所要做的就是重置环境变量IFS——内部字段分隔符——,这样它就不会按当前值(默认为空格)拆分行,而是按你自己的分隔符。

Considering an input file "a.csv", with the given contents:

考虑具有给定内容的输入文件“a.csv”:

1,2,3,4

2,3,4,5

6,3,2,1

1,2,3,4

2,3,4,5

6,3,2,1

You can do this:

你可以这样做:

IFS=','
while read f1 f2 f3 f4; do
    echo "fields[$f1 $f2 $f3 $f4]"
done < a.csv

And the output is:

输出是:

fields[1 2 3 4]

fields[2 3 4 5]

fields[6 3 2 1]

字段[1 2 3 4]

字段[2 3 4 5]

字段[6 3 2 1]

回答by TheRuss

A couple of good starting points for you are here: http://backreference.org/2010/04/17/csv-parsing-with-awk/

几个很好的起点在这里:http: //backreference.org/2010/04/17/csv-parsing-with-awk/