当前位置: > shell编程 >

对大文件进行分割的shell脚本

时间:2014-06-19 17:05来源:linux.it.net.cn 作者:IT网

系统运维中,日志文件往往非常大,此时就要求对日志文件进行分割。
本文介绍使用shell脚本对文件进行分割的二种方法,供大家参考。

方法一:
 

代码如下:
#!/bin/bash
linenum=`wc -l httperr8007.log| awk '{print $1}'`
n1=1
file=1
while [ $n1 -lt $linenum ]
do
n2=`expr $n1 + 999`
sed -n "${n1}, ${n2}p" httperr8007.log > file_$file.log
n1=`expr $n2 + 1`
file=`expr $file + 1`
done
 
其中httperr8007.log为你想分割的大文件,file_$file.log 为分割后的文件,最后为file_1.log,file_2.log,file_3.log……,分割完后的每个文件只有1000行(参数可以自己设置)

方法二:
split 参数:
-b :后面可接欲分割成的档案大小,可加单位,例如 b, k, m 等;
-l :以行数来进行分割;
 

代码如下:#
按每个文件1000行来分割除
split -l 1000 httperr8007.log httperr
httpaa,httpab,httpac ........
#按照每个文件100K来分割
split -b 100k httperr8007.log http
httpaa,httpab,httpac ........
 

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容