SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 4922|回复: 0

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

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑
; L7 h0 P& [2 ~9 k8 A" S3 d$ P: x: W) M" ?7 D5 {
sed命令通过关键字截取log。这里将时间转换参与逻辑运行。
1 S( s0 X, M7 J3 o提高log提取准确性。
4 Z: ~. w' A2 R9 F# z& a( ~0 D' K' C5 p* ~% c% k9 K
; l  m3 G# d2 Z
#!/usr/bin/perl
* r# w- L% W! J2 d% b1 _# A! v8 ~+ q$ s3 l
# Copyright Statement:% v. L. \6 @* x2 |1 ~: f$ Z, L
# --------------------
" g' \, X2 D9 v) J2 X8 Z: L; f* }' Z# This file is the tool to get the log
0 H/ U! f; |% {( A) U0 W  r# Between the time A and time B;9 W* l0 y. j: m# Z- ^
# Attention:Ensure the system have the sed commond!
0 c2 W# e& V& S( O# @Author: hakisman@gmail.com! a  |2 a9 {" @& L: k# ], I
# Date: 2014.2.17#; r0 L! L7 [8 L; a
7 S2 Y2 f+ U) F* O6 s
my $TargetFile = "$ARGV[0]"; " U" f% l5 U1 N; A8 Y6 n
my $StartTime = "$ARGV[1]";0 M: W& C0 z  ~
my $EndTime = "$ARGV[2]";* ]9 W" o6 `! M2 D9 v, j/ X
my $OutPutFile = "$ARGV[3]";4 @3 O# c# P! y9 W1 E, s+ C

  Z) s- U9 a1 B5 A% v+ o6 Vif($#ARGV<3){0 ~% e+ a2 o: E: j/ M
    print "ERROR:The number of parameters is wrong!\n";
- Y. b6 ]$ @8 p    &Usage;6 ?9 W9 S: k. B  I9 N- J% c! g
}- t4 ^; Z& A2 E1 z" d1 w  r% \
if(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){
, y5 V4 V) \% P, i$ |; E6 q  print "ERROR:The StartTime or Endtime is wrong!\n";
$ @: ?* }+ c$ k3 E, G5 T  &Usage;
* _) {( J( q0 C}
& C5 V" b) }& V
2 D+ x- W7 u5 V0 ?6 M* ?1 hmy $startLineNumber = 0;6 q; F$ e7 M* c8 d
my $endLineNumber = 0;
& ]2 o9 f5 x# |$ n8 q6 D
9 E) {) Y- |) L# z6 X# M4 p: qmy $timelocation = 0;4 d. g4 C. |* y! k
my $tempString;
4 t: l5 F$ M$ _1 P9 x: s/ ]' P4 O; `  z+ O
if(-d $file){
+ W9 s' q" G0 h- Z$ a5 R6 e/ h    print "ERROR:The TargetFile isn't exits!\n";+ M& M9 t9 }5 m2 i, Z9 ]1 [& W
    &Usage;1 M) v$ `' g) ~& _  L
}else{' e/ t& Z1 t9 ^9 V" l
    if(open (readfile,$TargetFile)){, O* C6 x) }; P5 X6 s
    }else{$ g; F1 E! G% ]( ~
      print "ERROR:Read file is faild!\n";
, V; c8 O. [) c  Y9 P; f" x        #dir ("Open the file faild!\n");5 O; w' ^* s, U
    }
1 F: J3 d5 i6 |. i    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!
8 Y' ?6 b  _5 L- c1 [# w: o    my @FileArray = <readfile>;/ w( ~6 [4 f8 J2 O  ^
   
9 w6 E3 m% h; o* F    my $filelength = $#FileArray;
% C* e1 ~. f) N7 v9 _   
) K3 x) D* ~3 D2 B/ Q6 |$ d" k    $endLineNumber = $filelength;
5 [3 [! ^# O3 s/ ^: c, E   
$ \) F6 b/ R, ^  V/ K! K; @* m! l; l    my $i = 0;
4 H$ O% P* u8 ^: b2 _
$ p8 \( S5 y- ]    while($i<=$filelength){) E' [9 W4 t& {! M9 T# J& V
        ) {8 w1 [  u% x9 v1 [
        $tempString = substr($FileArray[$i],10,5);. J$ T- n1 ~9 F2 p- I) r) l
        #print $tempString."------\n";
* H* H- L- @; v5 O+ S        if(($startLineNumber==0)&&(index($tempString,":")==2)){
3 }: r5 L: W, V4 F- B            $tempString =~ s/\://g;
7 u7 L5 `  [; H3 q9 f% ^& Y+ g                #print "$tempString\n";+ I9 f' @- T9 R2 B& y/ P4 f# s, t3 c3 A
                    if($StartTime<=$tempString){                        
3 ], O9 Y6 n0 X) a2 b, t. I                    $startLineNumber=$i;5 M! L* @1 a/ i0 {
            }
  n7 \/ k# w, H7 B        }
0 S# L7 U/ }) T) j3 _( B+ k" l: c- s        & _) Z! J6 R& g
        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){
0 F. C. o: R. R6 b3 I          $tempString =~ s/\://g;
6 H; a+ W0 H- D2 T1 B$ Z7 c1 ^          #print "EndTime is $EndTime"."____number1 is $number1\n";* F2 D: r8 b* K3 Z
          if($EndTime<=$tempString){
6 D# ?0 g* R1 P+ H              $endLineNumber=$i;   
$ J% }7 P9 l, I) g7 N& p  p              &getLog;
$ l5 L3 B0 @- }          }
0 q. p; y' N* X        }   
4 Y6 g9 m" E! V4 R        $i++;, S" T" Q5 u4 u" i0 M0 L4 h
    }
9 Z4 A) H1 a! [+ A; t) T* S: J    close(readfile);4 `+ I# f2 T2 a0 W; s
}+ k% _5 R/ S5 Y% B& B% Y- N

$ Q; M; y# V% R1 j9 N% y
7 D, S. f1 H4 S" W! l$ t6 Psub getLog{
& f0 i: @5 e# z8 ^/ K4 A- p
/ Q  t; l. r0 ~* d4 r  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";
/ n9 V" U3 R& X  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  1 Z: I2 d: C3 s" j
  print $command."\n";
% y' d. s8 T( f8 A   
7 x, A( y' p* c3 c1 W; ?" ]% t6 b4 l# d    system($command);5 U9 R9 m: i+ g( @( ]0 \
            
2 z' {* N" U* k: j; }    exit();- G1 J" {2 I, g4 {1 L: ~) x
}& F! z6 ]4 z9 p+ @. r

7 X) _- j1 {, i0 I. Z. tsub Usage {
4 u! [' u8 a! X" }" b4 t4 qwarn << "__END_OF_USAGE";3 }3 u0 K1 M5 i, p) R& ]& d0 ^. \5 c

& f! ]$ Y& g8 R3 O$ ^" Z$ yHoops! you need read the usage!# s7 e& E( W1 O1 ]
        # j' m. |( x& v3 I
Usage:( k/ F  f! @- s  s) k3 ^
. }* n6 ~& k: w- s- L0 i) @: r
  getlog TargetFile StartTime EndTime OutPutFile
( L. U; ]6 C" ]  Attention: For 24-hour time-division。
) ]& Z. u5 U. N# ]3 u
3 r' D- r: J: ZExample:
! a- \7 t4 E+ C  u0 R  getlog server.log 1530 1620 server_1530_1620.txt9 w/ }/ |1 x- A. x/ A& F5 j, M
  (Between the time A and time B)
; N4 G* W0 y! B8 ~0 H7 G  F% G  .....
9 C, r; O: a4 A: e3 }1 {3 \4 r* e$ p
  ^2 C0 a( a2 F( A__END_OF_USAGE
9 T; P0 x& q$ m6 P* t2 V   
* n" E/ ^- c9 }/ d  o1 iexit 1;+ b6 S9 Q4 Z" J0 m: Q4 `" G( Q6 |
}) \" S7 [) W- e: h( {6 V% D7 W

2 u- h! W. b& B" v; }$ J9 S3 Y- m/ m. m( |
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

SCMLife推荐上一条 /4 下一条

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

GMT+8, 2019-10-16 08:59 , Processed in 0.060433 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

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