SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 4289|回复: 0

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

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑
! W. M. F4 i4 P
1 |) B# _2 F. ]( L+ J: M. P' [sed命令通过关键字截取log。这里将时间转换参与逻辑运行。
* A/ _, `; Q! N6 ]) v  I提高log提取准确性。
- Z0 K& }( k2 d5 |% k# a! n- L+ N& R0 _& ^/ H" |
  D$ g" G4 F- K7 h) x
#!/usr/bin/perl
& J/ F$ A3 I7 h9 Z: N0 C9 c9 [7 _# k% W) h# }. J' I, {
# Copyright Statement:
7 P9 |/ ~6 ~+ q* B6 t- @# --------------------- i1 S  x( L- }6 e- Q9 `# @
# This file is the tool to get the log
. b) {3 ?( [- \: m7 M7 b& r- c" \# Between the time A and time B;
+ K0 q+ _, h/ j3 p# `6 Q: U$ N# Attention:Ensure the system have the sed commond!
* Y: M  \  C4 }9 H1 b% p6 Z- H. l& X# @Author: hakisman@gmail.com4 |2 r4 T& x4 a
# Date: 2014.2.17#% a+ }# w1 X. p& V
2 R7 I" k" O- q2 T
my $TargetFile = "$ARGV[0]";
) J% T) \1 A" ?8 n" g0 qmy $StartTime = "$ARGV[1]";
7 Z* g. w" P5 k' z3 r% q. Hmy $EndTime = "$ARGV[2]";
  v& ^5 U6 e5 r3 d- P: c) p( Imy $OutPutFile = "$ARGV[3]";
/ Y/ q; e6 G. E: ~& e3 \: `! u% L3 V$ |% B/ V# f% U5 V9 R; \& R
if($#ARGV<3){
3 b( L  b4 C& J1 Y; C( o4 W( @    print "ERROR:The number of parameters is wrong!\n";
5 n5 X1 K, {3 d0 W0 p& N    &Usage;, q3 w  N0 M  f" t' S% H7 P
}* N- j9 {$ K0 H7 v7 T& R& C. a1 y
if(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){
5 |+ s& `& j9 f. e; r1 A  print "ERROR:The StartTime or Endtime is wrong!\n";8 T" _9 d$ Z6 u( X# v: R* b2 Y( q
  &Usage;
/ q! y" M+ A1 W7 d) Z9 d2 T. R% U}+ b- @# D, h% g$ A

  v: S' O6 H3 Y+ A, y7 B; ^% jmy $startLineNumber = 0;
) i: k, e7 p9 z( u: Z' I! qmy $endLineNumber = 0;
: w* r7 ~$ {. c( j; W- |0 R
* {% N/ e" s0 O! Zmy $timelocation = 0;
1 V5 n/ y" D8 S4 e5 U: bmy $tempString;( l( g: w9 u& \9 Y. x# E
) D+ E/ {8 _0 n7 n+ \2 \# R  @
if(-d $file){" A! [8 n; V# t# E" h1 J
    print "ERROR:The TargetFile isn't exits!\n";
- B& \, D# y, }" e    &Usage;# K6 x$ X) `' `+ N
}else{: D0 }$ J  a9 T( M. L$ o
    if(open (readfile,$TargetFile)){
* d2 f( ^3 X. Y    }else{
5 v* a5 P9 H& E$ P% r" }& ?      print "ERROR:Read file is faild!\n";
% o2 E; u" ?8 J' m, h. q) t# m; H3 \        #dir ("Open the file faild!\n");
( D7 t- D: D; K$ l3 M% W3 o    }5 f" M* O8 L5 y1 D7 ?- Q
    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!( w5 b2 j0 H7 w( u. _% I# P
    my @FileArray = <readfile>;
- C# q/ ]4 W( p* b* w   
  V: q9 ]5 d; K    my $filelength = $#FileArray;
4 D2 e2 y+ E9 u8 x4 K    " V1 g0 m% i* P+ p+ O. C+ l, r
    $endLineNumber = $filelength;
) G- u% g5 a" L3 f8 g' C+ w   
2 o# o# R$ Y  x    my $i = 0;6 T9 L7 q, |9 H) H1 C7 e( q- b

2 ~- Q/ ?) L' P    while($i<=$filelength){) V3 i- B1 x1 p# [% i3 ~% [
        * S2 q% u# O  o' l$ x
        $tempString = substr($FileArray[$i],10,5);
) u1 A2 T6 r- ?' [2 R        #print $tempString."------\n";# w6 ~) x; d% d- q9 x
        if(($startLineNumber==0)&&(index($tempString,":")==2)){( V& }- n; Z' s) M
            $tempString =~ s/\://g;% ]  e1 l1 U( a
                #print "$tempString\n";
- `9 P1 `8 t8 w$ a/ v; G+ k, S                    if($StartTime<=$tempString){                        
7 ^" N6 A7 [1 ]2 a6 g8 A                    $startLineNumber=$i;
1 Y$ B# Y& d- L- x            }
8 |4 }" M8 M7 s( x3 Z5 U. P        }0 k- U$ Z) v2 c6 e
        
) E6 h5 q& F1 O3 M0 r" j        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){3 z& j% E  W8 t% n; F* `) y7 |
          $tempString =~ s/\://g;% s4 N8 p$ I$ O4 j' ~7 G
          #print "EndTime is $EndTime"."____number1 is $number1\n";7 W* T  ?. ]% f( [) Q+ n/ Y) b1 E
          if($EndTime<=$tempString){
) Z6 W* k7 f) s& t8 p8 F              $endLineNumber=$i;    8 `7 v8 G/ h) m+ c3 H; ^
              &getLog;) o+ _* B) M2 f* O
          }
8 _; d1 K" j/ D$ l% |8 k. D        }   
7 Z4 @; {- u( v7 O+ Z, K        $i++;
" `* Z/ _0 {& O- G( J    }
) i+ k, b: u% G. k    close(readfile);
6 D* ?, v+ ?! C2 N% g9 ^6 H$ u5 D3 G}
  h" C, f0 L, C9 ~& U
7 t, r1 M( N% ^$ \0 Z9 P" S
& d8 u- }( `9 o" c3 U/ Rsub getLog{% ]- e2 G6 W: `& j4 z1 A

9 \5 m" |% a) ~! R% T  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";# _+ K8 w- p$ Q+ F3 k) s0 Q
  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  
3 @( R) h7 U+ @! _! g& C: S% S( ?1 I  print $command."\n";2 A8 \0 H2 C1 y) g% t
   
! D. l  j$ k9 r" j    system($command);
; N4 m* d- }: E* h7 t+ V            
! c0 l$ c, H+ c1 s    exit();$ \; X9 g) O3 ^: N; V5 E
}" V) p3 h, E0 m0 o
1 h0 E( d2 Q' S$ |
sub Usage {
; @; h8 B' u* T0 w" p7 xwarn << "__END_OF_USAGE";
5 G- z" ~  X; s% y' B6 I; P* m
, J2 X, ^  ~$ D( EHoops! you need read the usage!0 o+ \( U  L2 ~5 B  g
        . Z+ j; D2 j1 @; k! E" ]- o7 @% i; \
Usage:
) ?4 h3 a( K5 u4 H5 |# V, g6 ?( f- a7 H6 c: s& k1 ]% x
  getlog TargetFile StartTime EndTime OutPutFile4 m9 x. h* r% t; H2 |
  Attention: For 24-hour time-division。
1 ]7 ?8 Z! p9 C# n9 q  q3 e% f
) m6 W1 i7 h% R) e9 b5 u" L  L' VExample:
1 J$ r& v( o$ _  getlog server.log 1530 1620 server_1530_1620.txt
! U6 ], Z6 N0 E. {: n: j9 s  (Between the time A and time B)0 V. j! L% w! Q# v; z2 L
  .....
! `" F6 f6 N( r5 {) n8 j& z: d% c9 t, Q  C0 y# H2 i# }' b: K* v8 B
__END_OF_USAGE6 y, C$ i; J# N9 ^  L
    ! W' d6 A, [0 }/ ~. F9 K' ]& q0 D
exit 1;
2 w: B0 p6 H3 z3 c- \}* ~9 U8 |! X" s- E

2 M$ A$ J% X8 u& U% W& g9 \
: C& Y) F% }6 ?+ v* E4 g& _# H
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

SCMLife推荐上一条 /4 下一条

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

GMT+8, 2018-12-17 14:14 , Processed in 0.059367 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

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