SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 4062|回复: 0

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

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑
' K7 a  F# o$ G, }7 [
) P! W" ~0 w  h9 u) @sed命令通过关键字截取log。这里将时间转换参与逻辑运行。
$ ^0 A( C% G( q! T' g提高log提取准确性。$ H& T, z: q& A

" Y: e% J3 s4 H6 l  x
- \. L3 J! h- [! Q7 j) I- U! u3 L#!/usr/bin/perl6 i/ d* Q1 x* p& T; D! ?

- p- x; D* P0 Z# Copyright Statement:  V) m$ ]9 y& L7 s( Q
# --------------------8 t/ t5 I: [( {  d
# This file is the tool to get the log5 g8 x! [2 y! f6 k* M, T6 D
# Between the time A and time B;
8 p0 I0 v  e* m% }# Attention:Ensure the system have the sed commond!2 K; y' |5 k. B$ q5 s
# @Author: hakisman@gmail.com0 c+ x6 K! [3 }# x  o* h3 J
# Date: 2014.2.17#
. D/ S6 F% n7 G) w
9 q! ]4 A9 m' v$ h) \" Qmy $TargetFile = "$ARGV[0]";
6 K, U; p9 N' Umy $StartTime = "$ARGV[1]";! s3 W: w# h, J& q4 J& Q
my $EndTime = "$ARGV[2]";: e+ H( ^9 e0 j; e  \
my $OutPutFile = "$ARGV[3]";
  w  g0 U0 I* k3 i( a: {# X, L* p: ]! v- T6 {$ ^* x8 U; q
if($#ARGV<3){; X' B$ m& w( ^, `
    print "ERROR:The number of parameters is wrong!\n";. e7 {% r6 B& g$ h" \
    &Usage;! v& a2 r' L: {$ T3 [* G8 Z
}- j7 c9 e. Q5 m
if(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){" G' }  s( {/ f
  print "ERROR:The StartTime or Endtime is wrong!\n";
0 G+ h* J+ f+ U1 A0 O* |4 `! w# q, C  &Usage;% _( P5 j9 H4 j& O4 Z5 ~
}
5 [# j# {, Z3 C" l
  F& [' U- R7 Xmy $startLineNumber = 0;
" ~5 G  O2 Z6 T$ Bmy $endLineNumber = 0;
6 m3 _) Z: i/ I1 W" o
* o' _+ P9 b6 ]+ G$ f9 g. dmy $timelocation = 0;
# c: o8 T: ^, Y. w; j& x0 Tmy $tempString;
3 d5 s7 V" ~4 M. v# Z" Y- W3 I( G! H5 U$ K7 a7 p! p3 N
if(-d $file){
5 V+ Z4 j, H# r4 z4 I0 N    print "ERROR:The TargetFile isn't exits!\n";
: \) B8 z0 w1 x" Q. f/ i3 Y    &Usage;
/ y" Y! K5 Q8 e6 o3 h% ]}else{
! L  j  L% ?+ Y7 J  H4 C$ Z. R6 }% O    if(open (readfile,$TargetFile)){
7 ~& o- J! p) M- ~    }else{. w* K; K% x5 F2 u' E. ]/ j
      print "ERROR:Read file is faild!\n";8 l/ q/ R  _/ p- a( R1 ?7 J9 M
        #dir ("Open the file faild!\n");% V! H# R! G0 G" ^* f' \
    }
, s% T% |, D: W( W    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!4 ]  ]6 W5 J$ i' k; e" v- v: V1 x7 ~
    my @FileArray = <readfile>;
5 \/ i3 p* v8 [+ K   
4 j- e: j9 H1 \0 ^9 w! w    my $filelength = $#FileArray;$ e$ U' j# O  i  j
   
# S- Y# l/ ]. [; }( I    $endLineNumber = $filelength;
. S$ L9 Q! Q' N  U7 [  C( |* ~2 k    7 Y. _8 j9 T) l: `, ~! G) p
    my $i = 0;$ C0 c) A+ j- R& F/ P

1 P6 E; L$ `# g: [3 t& w  p    while($i<=$filelength){7 L" `2 {7 K' @( D7 i
        ; }# D4 k$ _. ?' q
        $tempString = substr($FileArray[$i],10,5);# i8 W; s. \6 Z+ g: ^4 T
        #print $tempString."------\n";6 }4 u' ^2 s8 f) s, k* `* a
        if(($startLineNumber==0)&&(index($tempString,":")==2)){3 `6 l7 V# P: w( S- L4 @, L9 a
            $tempString =~ s/\://g;
1 |+ _) c. D2 X, p$ d9 e                #print "$tempString\n";
* y) \2 y3 D2 E3 _                    if($StartTime<=$tempString){                        4 R2 z1 |) f+ G' _; S) r
                    $startLineNumber=$i;1 B0 T6 g( d7 w# e7 P; t! W
            }
6 v9 A' m- C& t) \0 X2 P% a1 \        }
7 y3 N. m$ q. @9 A  p2 o6 s% x        # A! R5 S) v3 |+ f2 y
        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){
5 S" r: q; a6 A( X+ M% [( b( C          $tempString =~ s/\://g;
  J7 _3 ^% j6 t# n# O          #print "EndTime is $EndTime"."____number1 is $number1\n";- t1 G6 N' Z% P5 C
          if($EndTime<=$tempString){' k/ D- i* Y4 D# F, N& f
              $endLineNumber=$i;    4 Q9 u% s5 N4 z& s# R) {
              &getLog;
' w) Y. d% z. L  D1 s) }          }% z, F6 c' X7 y7 O
        }   
, p  n# E# a+ p5 [- ?" t5 ~) Y        $i++;
7 m* L0 Z" J3 g, B  R9 D    }
3 U7 n( D8 }$ M: k4 K* i) s    close(readfile);( u' N% f4 j  d* }5 ?
}
7 L- y1 A7 j6 I' O1 g
# |; c2 H* E- J- ~/ Y2 I4 ]: g' T0 @# d8 f: H- r9 I
sub getLog{
4 j- e9 B' ?) @! A- ^6 Z6 G: c8 `, E  q2 J. t7 z
  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";
" D7 n8 g" h7 W2 z4 E  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  
# X* i: w1 j+ m8 L  print $command."\n";/ x4 _5 [1 b8 t( U1 w
   
8 t9 k/ \4 o  F    system($command);# k( A( G, h" W& k) F
            
+ W. ?: [- L( z; y$ _+ g    exit();
! J) n- V. e3 ^/ t1 h}
9 A; m' D% l6 j9 {. ]4 {' `( |4 w; _$ v' u: P7 ^
sub Usage {
, a/ G: c4 \( d4 r* n9 twarn << "__END_OF_USAGE";* ^: P% F/ \/ E$ n' u
% W* G! T, Z5 g9 z
Hoops! you need read the usage!2 \: x* {" Q' h6 g
        
  D, _9 S$ ], h% J1 EUsage:
3 U$ P. i' |- _: [: d  L) e& W! v. k2 v0 _
  getlog TargetFile StartTime EndTime OutPutFile
9 |/ K/ R1 T' D  D; W3 g  Attention: For 24-hour time-division。
* d9 Q8 Y5 m, d7 Z
2 [- p4 h8 z2 y% ]  T8 l  m4 y! oExample:
8 j- ]; t8 r- n, [  getlog server.log 1530 1620 server_1530_1620.txt- ], U0 t$ |  @6 ~/ Q
  (Between the time A and time B)
7 ?& B5 K2 a% t, Z. W  .....6 u# g3 P% T) t% J) q1 t' |! w1 g  T

. F4 k1 r' P& @3 i2 m2 c2 r7 h__END_OF_USAGE; {: ?9 M+ h7 Q7 r5 _, M) F
   
0 D1 i5 G4 ]2 G. d: O% `+ n. H* Qexit 1;
  W, ^5 @" ?- U}
5 ?# i& X4 ~3 E
% e) A5 z; g7 w0 \
0 e- i: @" s0 f4 D( x9 S
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

SCMLife推荐上一条 /4 下一条

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

GMT+8, 2018-9-25 23:51 , Processed in 0.063048 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

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