我试图将包含太多行的 csv 文件拆分为用户定义的块。行数,使用以下脚本使用awk但它没有生成任何输出,文件已创建但它是空白的。请任何人指出缺少什么...
# !/bin/bash
File=filename.csv
count=`awk 'END {print NR}' $File`
i=2
j=5000
k=$j
while [ $j -le $count ]
do
awk 'NR==1 {print $0}' $File > output"$i".csv
awk 'NR==$i, NR==$j {print $0}' $File > output"$i".csv
i=`expr $j + 1`
j=`expr $i + $k`
if [ $j -gt $count ]
then
j=$count
k=`expr 0 - 1`
elif [ $j -eq $count]
then
break
fi
done
答案1
如果你真的想用的awk
话怎么样
awk -v c=5000 '
BEGIN{getline; head=$0; f = -1}
{if (f != int((NR-2)/c)) {f+=1; fl=sprintf("%03d.csv", f); print head > fl}; print $0 > fl}
' filename.csv
至于你的剧本......
#!/bin/bash
File=filename.csv
count=$(awk 'END {print NR}' $File) # syntax
i=2
j=5000
k=$j
while [ $j -le $count ]
do
awk 'NR==1 {print $0}' $File > output"$i".csv
# you cannot pass shell variables directly into awk, use -v
# and append >> don't overwrite >
awk -v st=$i -v fi=$j 'NR==st, NR==fi {print $0}' $File >> output"$i".csv
i=`expr $j + 1`
j=`expr $i + $k`
if [ $j -gt $count ]
then
j=$count
k=-1
elif [ $j -eq $count ] # needed a space before the ]
then
break
fi
done