SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 4485|回复: 0

[Perl] sed简单封装----将时间参与逻辑运算提取log

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑
; q' b0 B2 a1 |$ U  Z* r; u; X8 d0 U* ^, J7 _
sed命令通过关键字截取log。这里将时间转换参与逻辑运行。9 Y8 h# r) i4 R; w! ~
提高log提取准确性。$ C' x2 z# x* q, w

" Q8 D) j+ `% _) U
' l+ D" y- [) u) O; R0 Q5 w; I. c#!/usr/bin/perl
  w5 y* F+ g" L8 j# T
; J: Z7 u  M# a& Z3 F6 E! x# Copyright Statement:4 ^0 K$ c9 l7 M5 U6 d( l7 ]
# --------------------
, E0 J4 ~3 \  ]" ]# This file is the tool to get the log- }* D5 F* \3 [
# Between the time A and time B;# U, e* Q# m+ [. m
# Attention:Ensure the system have the sed commond!& a0 }) T4 A& v, y- Q& I0 }9 m7 f0 @
# @Author: hakisman@gmail.com5 p) d0 L# C. w. f
# Date: 2014.2.17#% L8 A! ~% m$ F: Y, T6 o4 T

8 A' ]4 v- e0 ~% ?my $TargetFile = "$ARGV[0]";
2 j9 c/ _. |/ ~2 W( @my $StartTime = "$ARGV[1]";! v! l0 s4 [4 m9 j1 o
my $EndTime = "$ARGV[2]";
5 ?* F" b  Z( L% I, I3 ^, omy $OutPutFile = "$ARGV[3]";
" z6 p9 u" x9 r4 c$ N; d
+ e) u8 A2 e3 D; ?: [2 o) Vif($#ARGV<3){9 l0 N1 v2 O! n, ?$ Q# G
    print "ERROR:The number of parameters is wrong!\n";) i# g7 {0 u3 l
    &Usage;# W; a% U6 X% `
}# ?7 d) D) x' v
if(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){
/ Q7 ?" K3 a' o& O4 V2 M  print "ERROR:The StartTime or Endtime is wrong!\n";
/ E& p1 G& W8 Z9 q5 ]& j% [" K  &Usage;- I) Y% S; V4 h4 G! x# q
}3 @! d6 s' a' P: F/ E

) U8 M& ~( F) wmy $startLineNumber = 0;
- z) T/ H# w* x$ b: q0 p" omy $endLineNumber = 0;7 v7 S) s3 @5 t2 h& n0 h; y& {. C
7 u4 s; G, U) X- {4 \0 \4 N& G3 h
my $timelocation = 0;$ Z' t! z- p, r
my $tempString;1 c: ]& D. W; O9 x

( W' q4 H( Y9 Lif(-d $file){
4 H/ m) r" z0 @0 E% s    print "ERROR:The TargetFile isn't exits!\n";
4 ?( W' ~. v8 h" U/ S    &Usage;) S( w9 z) M& v- m+ z" n
}else{1 m0 J2 [5 @3 |9 q' t0 m
    if(open (readfile,$TargetFile)){
" ?% p" c5 i( S5 y5 N    }else{
0 q1 S0 w0 T6 o' h* m, b- c- v) n. ]: D      print "ERROR:Read file is faild!\n";
6 I3 {3 W1 z' ^8 Q9 r! P        #dir ("Open the file faild!\n");( y6 P7 ^; @9 f% c" Z
    }
: R, @3 ]/ G/ b( S- `! ^9 Q    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!6 X4 F" I  o7 `% }% K" E
    my @FileArray = <readfile>;9 Z/ p# x5 N9 |% t; h
    * i8 _/ @7 P4 t! |/ ?
    my $filelength = $#FileArray;1 h1 N) N2 I6 e9 k  u3 z
    # R5 v" w0 p# z' b5 ?! ?
    $endLineNumber = $filelength;$ M/ U% k- Y# U) k" Y
   
4 a0 ^5 R# z9 m+ o  }    my $i = 0;0 R; Y- ?. }" _! r) Z5 e
* k. B9 e* t* F4 n5 Z) S( B
    while($i<=$filelength){  a" m: V; g+ s: O& W2 B
        - y# @( ~/ i( M
        $tempString = substr($FileArray[$i],10,5);0 H9 ]4 ?9 G" B
        #print $tempString."------\n";1 o& a3 ^- b% r5 y
        if(($startLineNumber==0)&&(index($tempString,":")==2)){6 J# _( g" m) u5 m8 s' k
            $tempString =~ s/\://g;
0 P2 G! r$ n; v3 g* A, H                #print "$tempString\n";
% u4 p  M7 F7 [& y1 \, y# L  y/ {. E                    if($StartTime<=$tempString){                        
# J- f5 Q0 o& j) m* N% e                    $startLineNumber=$i;
# N7 \; X2 d. ], B            }5 ]' @$ _- B5 r9 q" m5 L% \7 X& |! p
        }( Y/ N8 d+ E$ ?
        
4 Z5 K+ w6 {7 i& v        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){
' _' @  @. [, q+ o          $tempString =~ s/\://g;- f, R9 D% ~+ v
          #print "EndTime is $EndTime"."____number1 is $number1\n";6 r* y+ G2 \" z+ ]+ D3 j
          if($EndTime<=$tempString){" X/ l7 h8 @) A7 X1 r* ?# l" p
              $endLineNumber=$i;    # N; G! G  L2 F1 @6 e
              &getLog;; x/ q* y4 \! G) X6 C; U+ U
          }9 l1 x: O+ _9 P1 P6 O! C" U8 P
        }   
: u  a- k! q% I! k$ m: C2 P. t+ p        $i++;
2 Z  g0 u9 f$ e" Q2 B3 D    }
1 h' d$ `( H8 h) V8 e" m  x    close(readfile);
6 w/ ?1 {4 ~- o  q, ^) W: U}
0 T, T! C/ t; Z2 `/ \+ g$ h
0 J3 ^5 T4 O; `. Y3 m8 ?/ j" D: }3 q; R3 R8 o2 X* K- d1 d; }
sub getLog{' i* q3 q9 `* C9 L
  [9 b6 y* |: W& B* k( {$ d- \2 e5 \
  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";- b5 v+ U7 q, e7 s9 _* M
  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  
2 P0 Q: W4 g0 W3 }7 P  p5 w4 f: e  print $command."\n";
. ]- H8 r( n6 r- Z5 G7 ~5 }    5 d2 x8 m" r0 `) \
    system($command);* E% j+ c9 d- |$ H
            
/ l/ u- U& [) {) b& H    exit();
& E0 q$ M$ l# l( [' ~}/ o& L) l/ T& U
1 {4 U+ M) m: I! r) R3 k& @$ i
sub Usage {
/ _. B1 R& k5 l- H2 A, Ewarn << "__END_OF_USAGE";
& R9 Y& B* x8 A7 |9 t5 D7 ]' E9 t) v0 E3 P; Z
Hoops! you need read the usage!
8 I( ?2 o; r" v8 |9 u* X0 ]- c$ D        
( P" |) F" Z* }Usage:. E) }1 R/ q. I9 T
2 O( N* `; Q# J. A; M2 c
  getlog TargetFile StartTime EndTime OutPutFile
4 G2 w" ^% ?" v# D  Attention: For 24-hour time-division。! m- D1 k6 k; j1 l

7 H! N6 s9 P6 M/ I6 JExample:
' x1 H# k$ e% E& ]* G/ H9 `% e+ B  getlog server.log 1530 1620 server_1530_1620.txt
4 L% a  F/ y- Y  j/ s) W  (Between the time A and time B)$ c- J& N5 J" {  e' \
  .....# _+ @! z7 G) G# [0 w0 }8 }
' r3 X* J4 x0 \& s7 r0 l8 }' y
__END_OF_USAGE
. o5 X* R( F3 [+ Y9 s) H0 o4 f    * O8 Y! m) f' ]' [3 i- I0 o
exit 1;
" @. {7 n9 s, a. o}- e& A3 L. K1 r1 h3 [3 P

" l7 O* M1 M0 c7 p2 a
- D; f9 R& i/ t, @5 y( a7 V
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

SCMLife推荐上一条 /4 下一条

QQ|小黑屋|手机版|无图版|SCMLife.com ( 京ICP备06056490号-1 )

GMT+8, 2019-2-19 18:02 , Processed in 0.059467 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

快速回复 返回顶部 返回列表