SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 5882|回复: 0

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

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑 1 p* p2 m7 i* L  b

+ G/ V. r% ?# h, O  Vsed命令通过关键字截取log。这里将时间转换参与逻辑运行。* r1 }  P7 J/ C: G
提高log提取准确性。$ V( n6 S3 X9 B- _
) e3 @9 S: O3 M# u% C9 D
5 H0 W2 {/ r, l9 a3 _. R
#!/usr/bin/perl
/ E( t8 P# o3 B
" Y% @/ x, }  {7 Q9 G6 r0 B) a# Copyright Statement:
9 f2 t1 v1 W* s6 p# --------------------* c8 P6 j4 H0 N, q
# This file is the tool to get the log) I- D  E$ j' A% b" }3 @; V6 {: A6 j
# Between the time A and time B;6 O# U. r& c. |/ D! f* H
# Attention:Ensure the system have the sed commond!) v0 Q. c6 V2 f  l$ b
# @Author: hakisman@gmail.com
2 p; q* A8 g. U# Date: 2014.2.17#
" @+ V. n$ s6 Z0 j0 n2 c3 Y$ V) o# p" `
my $TargetFile = "$ARGV[0]";
  i  ^" Y6 U: B, ~7 }my $StartTime = "$ARGV[1]";
5 b% n, s: ^+ e: U. c6 m4 Tmy $EndTime = "$ARGV[2]";+ ~! C, Q; V8 D% |, q+ g- c
my $OutPutFile = "$ARGV[3]";, K& N9 n0 K: M; D) ~' f

2 X( w# q' a7 N  {0 i; vif($#ARGV<3){! Z( T9 r. f! L0 s2 q1 \; N+ X$ M5 o
    print "ERROR:The number of parameters is wrong!\n";% U1 I: ?+ J! a) E# J' W
    &Usage;
4 G2 ^0 e6 O* z5 w. ?0 \}
  V6 T) T- v' F  nif(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){5 S6 i0 c) x3 Q9 }, l
  print "ERROR:The StartTime or Endtime is wrong!\n";/ j/ F+ e" q- A1 \) C$ Q
  &Usage;/ d. {) T* u+ v; n2 P
}
# }$ }5 X% c5 |0 }( L: B* ]- `6 F
my $startLineNumber = 0;' {1 o( H( ~6 l& G; s( Z" J
my $endLineNumber = 0;0 w, k2 o) Z. Q4 s3 q

2 v$ N2 S" G+ b4 x/ E8 x- D) qmy $timelocation = 0;
: ]* Z' j; f, [& S: c: w/ Omy $tempString;; Y. s& j) [! N
  X# q% A' |) T& r. D0 g
if(-d $file){9 d8 ]3 q4 n. Y: L+ c
    print "ERROR:The TargetFile isn't exits!\n";) [% h* m& \5 {+ N4 B0 h
    &Usage;& ^8 k: N& J+ P/ U% t; |+ M
}else{
' }& x( n4 E( {& Q0 n3 D    if(open (readfile,$TargetFile)){4 L- m5 q7 s8 z  u
    }else{$ }) L5 i: }7 q3 K$ ~  r& r
      print "ERROR:Read file is faild!\n";7 F6 P3 d+ z  ]
        #dir ("Open the file faild!\n");
7 O$ {/ ?- K+ m* ^8 j    }' L7 C0 \# C. \( f( F: S
    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!
1 I  f7 \2 N# A: z    my @FileArray = <readfile>;
1 G+ e4 l; U' x+ R5 e" K    ; s- ?- f, t! f4 W/ \! e' w6 {$ d
    my $filelength = $#FileArray;1 V+ s1 b$ D- c5 d% z4 y0 U, m
   
# ?/ P! V1 z) M3 i' ]5 q    $endLineNumber = $filelength;, T# U  A- n/ c: [) ~
   
" e4 j8 t* n& h    my $i = 0;5 l4 [, e9 h5 N, H9 d. G" L6 x

, B- p' X9 g/ Z. v8 d    while($i<=$filelength){+ v5 a; Y5 e0 o8 x+ O
        3 k7 Q  h0 i+ H$ p& t
        $tempString = substr($FileArray[$i],10,5);4 V  I6 ]& z1 L- d2 y- w' B
        #print $tempString."------\n";: n8 B' e: n0 |9 z
        if(($startLineNumber==0)&&(index($tempString,":")==2)){9 Z8 L0 A9 [8 ^* E4 J1 K( V
            $tempString =~ s/\://g;" r& M+ ]* w( n$ M6 }, Q
                #print "$tempString\n";
4 \3 f6 m: H4 {3 a9 Q! R" F                    if($StartTime<=$tempString){                        
- P8 z+ _- r! g9 z/ ?9 [                    $startLineNumber=$i;. L9 x) u; H  v! f) i" i+ u! Z  S
            }9 \+ \$ z( d5 E* M& _: G4 K
        }. _' @4 z8 a0 E  y4 ~1 Y6 a
        
2 f% D7 K& h& S8 H        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){
+ t$ V" p2 m3 z5 K/ \          $tempString =~ s/\://g;
$ X1 w) C( x; t6 f) Q( `          #print "EndTime is $EndTime"."____number1 is $number1\n";. T/ a; I) }8 R
          if($EndTime<=$tempString){
# v6 [! D( ~! ^              $endLineNumber=$i;    # g# l; O  F* F( h1 P
              &getLog;
. O) i: z5 A7 v+ h3 r& o          }7 y8 g- @5 t/ Q% Q" J
        }    ! `3 J4 P) Y; D  `) R
        $i++;* A8 G: r( H' u# Z# X: P- [2 x
    }' ]3 @( \3 G7 t
    close(readfile);
6 J' S6 ]9 o3 j. Z9 w5 Z}
' m# r3 _1 D+ e  s! P; ?" n
1 {( I* s2 A; o: P; M! W
% Q* g7 I; V0 z6 S: xsub getLog{8 V* e7 X- L! k" w2 N5 ^
$ f7 U; |% k8 A, K0 i' y
  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";
' i+ ]: d. J4 u, B$ t/ P0 s  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  6 o7 Y# j0 ?8 [3 J+ H; r' x4 K
  print $command."\n";
1 e+ _; ?) y4 c: b% H! G0 P( L      ?9 z. h8 _0 x! @: E" g
    system($command);
. P& N+ }' C! H            0 T/ V: M1 ^& }9 [2 [$ h/ Q
    exit();
4 f: N3 i1 K  T" }}
. q$ L2 J5 \/ j
( G% O- v5 {: d, ?# l# Y8 @sub Usage {4 {! R2 X5 a% b
warn << "__END_OF_USAGE";
( ~+ K  h$ @% k% ~" p. _: {' R) L/ g0 a0 c: `7 F
Hoops! you need read the usage!' E2 ^" I  e" H" D% i
        
$ V; j0 N0 ^1 tUsage:2 z# o# I  v$ l4 R  F* z1 m. r

, B" l; Q0 {. e7 j6 T; g0 e  getlog TargetFile StartTime EndTime OutPutFile
) q% e) e" O9 F: f8 E  Attention: For 24-hour time-division。
8 u0 d3 S" j4 B! h  b: ~. C" F! A1 R- `% V# V$ b; u
Example:5 V1 v5 u7 [4 V1 a% H
  getlog server.log 1530 1620 server_1530_1620.txt: U- S% c, h; m: b
  (Between the time A and time B), q9 i( W1 X& `9 e
  .....& t, Z: P4 D$ m& g! B

" x1 g- I% l- r' P* U" x8 A__END_OF_USAGE
* M- v+ |# ^+ T2 u5 L    / {, U1 ^5 E( k* k
exit 1;: c# [9 a* @+ w* X+ e
}
3 M; q  L; R6 \( g" r, z0 ~; O: \. k) a  n" n5 i2 I7 s' I" l

& p* }  ^9 t4 K$ I
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

SCMLife推荐上一条 /1 下一条

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

GMT+8, 2022-8-18 19:35 , Processed in 0.064368 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

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