【发布时间】:2013-11-09 21:21:01
【问题描述】:
有一个 CSV 文件有一些列,第一列是 5 位客户编号,其他列用“;”分隔
这是一个例子:
12345;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
67890;some;other;cols;comes;here
34567;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
24315;some;other;cols;comes;here
如果第一列是空的,我需要设置最后一个给定的客户 ID。结果应如下所示:
12345;some;other;cols;comes;here
12345;some;other;cols;comes;here
12345;some;other;cols;comes;here
67890;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
24315;some;other;cols;comes;here
现在我使用 bash 脚本逐行读取文件,并想检查该行是否以数字开头。如果是,则用“;”爆炸该行并使用 array[0] (第一个值)设置 customerID。接下来,我检查该行是否不是以数字开头,并想在该行的开头写五个数字。但我无法使用客户 ID 访问数组索引。
这是我的脚本:
#!/bin/bash
while read line
do
row=$line
if echo $row |grep "^[0-9].*$" > /dev/null;
then
arr=$(echo $row | tr ";" "\n")
echo ${arr[0]};
fi
done < $1
我得到整行没有“;”而不是作为 arr[0] 的 CustomerID 接下来我不知道如何将行首的数字写回文件。有人可以帮帮我吗?
【问题讨论】: