功能
将某一目录下的所有文件分隔为相同行数的文件
实现
INDIR=./in
OUTDIR=./out
FILE_MAX_LINE=100000
CUR_LINE_NUM=0
CUR_MATCH_FILE_IDX=0
MSG_TPYE=ccr
# define and clear tmp file
TMP_FILE=$OUTDIR/get_match_file.tmp
rm -f $TMP_FILE
# split file
ls -lrt $INDIR | grep ^- | awk '{print $NF}'| while read filename
do
filepath=$INDIR/$filename
LINE_NUM=`wc -l $filepath | awk '{print $1}'`
CUR_LINE_NUM=`echo "$CUR_LINE_NUM+$LINE_NUM" | bc`
cat $filepath >> $TMP_FILE
while [ $CUR_LINE_NUM -ge $FILE_MAX_LINE ]
do
head -n $FILE_MAX_LINE $TMP_FILE > $OUTDIR/p${MSG_TPYE}.${CUR_MATCH_FILE_IDX}
sed -i '1,'${FILE_MAX_LINE}'d' $TMP_FILE
CUR_MATCH_FILE_IDX=`echo "$CUR_MATCH_FILE_IDX+1" | bc`
CUR_LINE_NUM=`echo "$CUR_LINE_NUM-$FILE_MAX_LINE" | bc`
done
done
# output last file
cat $TMP_FILE > $OUTDIR/p${MSG_TPYE}.${CUR_MATCH_FILE_IDX}
rm -f $TMP_FILE