bash shell数组输出范围到csv。

时间:2020-12-28 15:42:33

Is there an easier way to do the below? I am reading in a large cvs file and want to only display ranges on the index per line and output back into a csv format.

有更简单的方法来做下面的事情吗?我正在读取一个大型cvs文件,并希望只显示每行索引上的范围,并将输出显示回csv格式。

while  IFS=$',' read -r -a myArray
do
    echo ${myArray[44]},${myArray[45]},${myArray[46]},${myArray[47]},${myArray[48]},${myArray[65]},${myArray[66]},${myArray[67]}
done < $SHEET

3 个解决方案

#1


4  

You can use the substring operator with array parameter expansion:

可以使用带有数组参数展开的子字符串操作符:

while IFS=, read -r -a myArray
do
    ( IFS=,; echo "${myArray[*]:44:5},${myArray[*]:65:3}" )
done

#2


2  

awk 'BEGIN{FS=OFS=","}{print $44, $45, $46, $47, $48, $65, $66, $67}' "$SHEET"

We set the Input Field Separator (FS) and Output Field Separator (OFS) to , so that output will be in csv format.

我们将输入字段分隔符(FS)和输出字段分隔符(OFS)设置为,以便输出是csv格式。

#3


0  

Looks like this is tailor-made job of awk. Use this awk command:

看起来这是为awk量身定做的。使用awk命令:

awk -F"," '{printf("%s,%s,%s,%s,%s,%s,%s,%s\n", 
            $44, $45, $46, $47, $48, $65, $66, $67)}' $SHEET

#1


4  

You can use the substring operator with array parameter expansion:

可以使用带有数组参数展开的子字符串操作符:

while IFS=, read -r -a myArray
do
    ( IFS=,; echo "${myArray[*]:44:5},${myArray[*]:65:3}" )
done

#2


2  

awk 'BEGIN{FS=OFS=","}{print $44, $45, $46, $47, $48, $65, $66, $67}' "$SHEET"

We set the Input Field Separator (FS) and Output Field Separator (OFS) to , so that output will be in csv format.

我们将输入字段分隔符(FS)和输出字段分隔符(OFS)设置为,以便输出是csv格式。

#3


0  

Looks like this is tailor-made job of awk. Use this awk command:

看起来这是为awk量身定做的。使用awk命令:

awk -F"," '{printf("%s,%s,%s,%s,%s,%s,%s,%s\n", 
            $44, $45, $46, $47, $48, $65, $66, $67)}' $SHEET