取出一天9:00~21:00每小时的随机100条日志。用/var/log/messages举例
代码具体如下:
- #!/usr/bin/bash
- #取出一天9:00~21:00每小时的随机100条日志。用/var/log/messages举例。
- #日志文件是一天内的日志,如果日志文件中包含多天的日志,还需进行一些修改!否则是会有些错误!
- LOGFILE="/var/log/messages"
- CURDIR=`pwd`
- DATE=`date '+%Y_%m_%d'`
- SAVEFILE="$CURDIR/day_random_log.$DATE"
- seek_date(){
- startline=`awk -F'[ :]' -v line=$1 '$3 == line {print NR;exit}' $LOGFILE`
- endline=`awk -F'[ :]' -v line=$1 '$3 == line {endline=NR}END{print endline}' $LOGFILE`
- }
- exec 5>&1
- exec > $SAVEFILE
- for i in `seq 9 21`
- do
- seek_date $i
- awk -v startnum=$startline -v endnum=$endline -f $CURDIR/hour_log.awk $LOGFILE
- done
- exec 1>&5
- exec 5>&-
- #!/usr/bin/awk
- #随机抽出文本中的100条日志
- BEGIN{
- delete per_hour_log
- line=0
- }
- NR >= startnum && NR <= endnum{
- line++
- per_hour_log[line]=$0
- }
- END{
- srand()
- if (line < 100)
- for ( i in per_hour_log)
- print per_hour_log[i]
- else
- {sum=0
- while (sum <= 99){
- linenum=int ( line * rand() )+1
- if ( linenum in per_hour_log ) {
- print per_hour_log[linenum]
- delete per_hour_log[linenum]
- sum++
- }
- }
- }
- }
阅读(2046) | 评论(0) | 转发(1) |