今天要切割一个很大的日志文件,这个日志文件已经是按每天切割过的了,但是仍然很大,内存不足无法处理,所以需要进一步把这个文件按行切割。在网上搜索好久,终于找到一个高手的脚本,在这里感谢他,此处为脚本的转载,一起学习。
#!/bin/sh
#print arguments
echo "argument 1 is:"$1
echo "argument 2 is:"$2
#variable initialization
file_no=0
curr_len=$2
total_len=$(cat $1|wc -l)
echo "total length is:"$total_len
#cut lines in file
while [ "$curr_len" -le "$total_len" ]
do
echo "current length is:"$curr_len
head -n $curr_len $1|tail -n $2 > $1$file_no
let file_no++
curr_len=$(expr $curr_len + $2)
done
#if total_len mod curr_len = 0
if [ $(expr $curr_len - $2) -eq $total_len ]
then
exit
fi
#cut rest lines in file
if [ "$curr_len" -gt "$total_len" ]
then
echo "cut rest file"
head -n $curr_len $1|tail -n $(expr $2 - $curr_len + $total_len ) > $1$file_no
fi