有系統運維的過程中,日誌文件往往非常大,這樣就要求對日誌文件進行分割,在此特用shell腳本對文件進行分割
sed的用法
#!/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
split的用法
split 參數:
-b :後面可接欲分割成的檔案大小,可加單位,例如 b, k, m 等;
-l :以行數來進行分割;
-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 ........
結合awk的用法
name="76868&5676&435&43526&334&12312312&12321"
echo|awk 'BEGIN {split('"\"$name\""', filearray, "&")}{for(i in filearray)print filearray[i]}'
echo a b c d|awk 'BEGIN {OFS=":"}{print $1,$2,$3,$4}'