我在名为 abc.txt 的文件中有一条记录,如下所示:
ID, date, timestamp, count, idcount, unit, code, Pcode, ccode, bid, vcode
12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
我想写一个unix shell代码来生成更多的no。只需增加列 ID、Pcode 和 ccode 即可记录记录,其余列保持原样。
12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345433,10-11-2011,11:11:12.555,0,0,XVC_AS,12,15,20,123454323,qweds
12345434,10-11-2011,11:11:12.555,0,0,XVC_AS,12,16,21,123454323,qweds
12345435,10-11-2011,11:11:12.555,0,0,XVC_AS,12,17,22,123454323,qweds
12345436,10-11-2011,11:11:12.555,0,0,XVC_AS,12,18,23,123454323,qweds
12345437,10-11-2011,11:11:12.555,0,0,XVC_AS,12,19,24,123454323,qweds
.
.
.
.
1000 times or n times
答案1
我假设问题中的输入和输出中的空行实际上并不存在。使用磨坊主( mlr
),首先清理标头中存在的额外空格,然后创建$n
单个数据记录的副本。然后,我们将其传递给第二个 Miller 调用来更新记录,增加 nameID
和列。Pcode
ccode
mlr --csv clean-whitespace then bootstrap -n "$n" file |
mlr --csv put '
NR == 1 { @ID=$ID; @Pcode=$Pcode; @ccode=$ccode }
NR > 1 {
$ID = @ID + NR - 1;
$Pcode = @Pcode + NR - 1;
$ccode = @ccode + NR - 1;
}'
给定问题中的数据以及n=10
shell 中的数据,这将输出
ID,date,timestamp,count,idcount,unit,code,Pcode,ccode,bid,vcode
12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345433,10-11-2011,11:11:12.555,0,0,XVC_AS,12,15,20,123454323,qweds
12345434,10-11-2011,11:11:12.555,0,0,XVC_AS,12,16,21,123454323,qweds
12345435,10-11-2011,11:11:12.555,0,0,XVC_AS,12,17,22,123454323,qweds
12345436,10-11-2011,11:11:12.555,0,0,XVC_AS,12,18,23,123454323,qweds
12345437,10-11-2011,11:11:12.555,0,0,XVC_AS,12,19,24,123454323,qweds
12345438,10-11-2011,11:11:12.555,0,0,XVC_AS,12,20,25,123454323,qweds
12345439,10-11-2011,11:11:12.555,0,0,XVC_AS,12,21,26,123454323,qweds
12345440,10-11-2011,11:11:12.555,0,0,XVC_AS,12,22,27,123454323,qweds
12345441,10-11-2011,11:11:12.555,0,0,XVC_AS,12,23,28,123454323,qweds
使用awk
并忽略标头的清理,并假设我们知道要更新的字段是字段 1、8 和 9。此外,由于awk
不支持 CSV,因此我们必须假设我们正在处理“简单 CSV”数据,即不包含嵌入逗号或换行符的字段的数据。
awk -v n="$n" '
BEGIN { OFS=FS="," }
NR==1 { print; next }
{
for (i=1; i<=n; ++i) {
print
$1++; $8++; $9++
}
}' file
答案2
$ awk -v n=10 '
BEGIN { FS=OFS="," }
{ print }
NR > 1 { for (i=1; i<=n; i++) { $1++; $8++; $9++; print } }
' abc.txt
ID, date, timestamp, count, idcount, unit, code, Pcode, ccode, bid, vcode
12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345433,10-11-2011,11:11:12.555,0,0,XVC_AS,12,15,20,123454323,qweds
12345434,10-11-2011,11:11:12.555,0,0,XVC_AS,12,16,21,123454323,qweds
12345435,10-11-2011,11:11:12.555,0,0,XVC_AS,12,17,22,123454323,qweds
12345436,10-11-2011,11:11:12.555,0,0,XVC_AS,12,18,23,123454323,qweds
12345437,10-11-2011,11:11:12.555,0,0,XVC_AS,12,19,24,123454323,qweds
12345438,10-11-2011,11:11:12.555,0,0,XVC_AS,12,20,25,123454323,qweds
12345439,10-11-2011,11:11:12.555,0,0,XVC_AS,12,21,26,123454323,qweds
12345440,10-11-2011,11:11:12.555,0,0,XVC_AS,12,22,27,123454323,qweds
12345441,10-11-2011,11:11:12.555,0,0,XVC_AS,12,23,28,123454323,qweds
12345442,10-11-2011,11:11:12.555,0,0,XVC_AS,12,24,29,123454323,qweds
答案3
它是否必须是外壳,或者例如可以awk
吗?喜欢
awk -F, -v"CNT=10" 'NR==1; NR==2 {for (;CNT--;) {print; $1++; $8++; $9++}}' OFS=, file
ID, date, timestamp, count, idcount, unit, code, Pcode, ccode, bid, vcode
12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345433,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345434,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345435,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345436,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345437,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345438,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345439,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345440,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345441,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
CNTprint
用递增的第一个字段乘以第二行。
如果必须是外壳,请尝试
OLDIFS=$IFS
CNT=10
{ read LINE; echo $LINE; read LINE; } <file1
IFS=, ARR=($LINE)
while ((CNT--))
do ((ARR[0]++ + ARR[7]++ + ARR[8]++))
echo "${ARR[*]}"
done
IFS=$OLDIFS
确保预先设置计数,否则您最终会得到相当大的结果文件......