SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 4630|回复: 0

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

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑
" F/ o- m4 x- E- `8 `5 L' c9 M/ Z9 c7 y
sed命令通过关键字截取log。这里将时间转换参与逻辑运行。
3 ^# h: J3 A6 f0 g  }/ \提高log提取准确性。; q. i2 ~# D7 @# a
3 e: d) [4 b3 o3 Z/ u1 t

) l+ B# W; ~/ `#!/usr/bin/perl
1 I. s( a8 P& t6 q: d) E
9 U# ?" v+ i: Y% D# Copyright Statement:6 i( P0 a9 K* [: Q
# --------------------9 F: j/ \1 \  A8 s$ d: A
# This file is the tool to get the log
& e% I6 X$ R) Z  \, P) F1 c# Between the time A and time B;
6 I3 Y) b% l' V. I7 Z" e! ~2 p# Attention:Ensure the system have the sed commond!
3 q  O' S. {: c5 |% g# @Author: hakisman@gmail.com, V& t( n+ l, G, x
# Date: 2014.2.17#
( z) v1 z2 b6 n: I% q  y
0 f; b, Z' r; p8 Vmy $TargetFile = "$ARGV[0]";
/ \% e3 V9 p! ~" Y, Gmy $StartTime = "$ARGV[1]";- l2 D" L2 \& m4 Y
my $EndTime = "$ARGV[2]";
' g7 u" O' K5 z6 n# p0 X% Wmy $OutPutFile = "$ARGV[3]";
2 D7 k) J, Y9 I/ S; [. K, M$ W; C5 o7 Q
if($#ARGV<3){1 H* L6 A- K! n4 J8 j9 }
    print "ERROR:The number of parameters is wrong!\n";
& G6 _- N' }$ r* ~% T    &Usage;
% \; n3 ]% @# y1 j- E2 V) n}
7 _1 e& m" [3 s% t; }4 i4 Zif(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){
4 Q$ b: |2 L; o9 s: \7 w: {  print "ERROR:The StartTime or Endtime is wrong!\n";
5 _9 G; q+ u7 J) l0 |5 c8 ?/ h% ^. T$ Z  &Usage;
, a+ t+ Q  ?6 N5 @; N1 y7 r6 ?' K7 v9 D}' e8 t% ~6 m2 i! d
* O. K; {1 Z! E2 O
my $startLineNumber = 0;9 D8 y" U" ~  a: @  Y1 T3 Z9 ?
my $endLineNumber = 0;. W% b4 {- T# n: z! n$ {4 _4 V1 W
+ e3 Y* B  p- u" x
my $timelocation = 0;
* S  u8 X2 W. w. |/ V* y6 ~my $tempString;' m3 Y" g: c3 j( \9 b" e- Q' P4 K

& h6 h- `9 B2 T- T( R! Uif(-d $file){
& k! C; |3 C7 f6 D; M7 E    print "ERROR:The TargetFile isn't exits!\n";6 k  _2 \5 w' O4 g' p; {
    &Usage;& ~' }/ G: x* B  k% f1 |
}else{2 q9 a  o  w& m- L  E+ T. V
    if(open (readfile,$TargetFile)){
& d& ?+ R7 H- \) [; f. z/ I% m    }else{9 @7 p$ Y4 _% S, H
      print "ERROR:Read file is faild!\n";+ c  R5 [8 D. k
        #dir ("Open the file faild!\n");
0 i. Q3 _5 w: d# \7 x$ w    }
1 Y, Y. L. l& ]- \( K$ d    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!
: T* r. q# \# a* S( O2 w    my @FileArray = <readfile>;
2 Q( g0 ?- ^3 X    ! R1 N/ B  Y+ j( g( W# s
    my $filelength = $#FileArray;# U- N# X2 H2 |# @# A, P9 x  t' A
    ! h1 K$ ~% S. r4 L* B
    $endLineNumber = $filelength;3 G4 Z" @0 L/ S' b6 l1 c# T
   
: T2 l2 o! L* x% a% p$ J- ~% k. }    my $i = 0;& s* j: u$ d1 J; u( O- t3 l
" s, S2 A2 [" B; c  ~( A" u5 E* Y
    while($i<=$filelength){. a6 g* o0 A# o# i2 T* h
        
/ t( t: j3 p( `1 i        $tempString = substr($FileArray[$i],10,5);
8 y; q% }) L, I' b' A        #print $tempString."------\n";
# G6 v& Y6 v$ J/ k4 v# w        if(($startLineNumber==0)&&(index($tempString,":")==2)){
5 q1 U: P& B9 `2 J: H            $tempString =~ s/\://g;) f, u* c* p7 u+ o  C
                #print "$tempString\n";6 S" q2 k& O4 C; o
                    if($StartTime<=$tempString){                        
" ~: H) @* b# M0 Y7 k& H                    $startLineNumber=$i;
$ O& S) D# x. r/ a5 i! m            }
9 F' g/ G( H( F7 k. T  B6 x        }
# }7 e& B6 F6 O2 e5 N. W- s7 E        + `$ ]& c: Y5 P0 E
        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){
. P) P% A  K+ s6 O6 v+ \& Z          $tempString =~ s/\://g;' Q; v% D0 }& d2 B/ k
          #print "EndTime is $EndTime"."____number1 is $number1\n";
; `5 l) M) M) m" ^          if($EndTime<=$tempString){. j  H" Z3 |0 M2 L- p
              $endLineNumber=$i;   
" \8 F" E  x/ w" K) Q              &getLog;4 V8 v6 f# P0 g7 `8 B8 D
          }
8 d! p/ K8 O4 ?' V) E* m1 f+ c        }   
9 Z! P7 @. t4 g0 ~) J& t# V        $i++;
5 x; P# G2 b1 S2 L: Z0 `3 m    }. C+ M" b  P7 d- s
    close(readfile);
# U2 y+ @5 b% Y/ O8 Z}
4 g5 W$ b% H- F* b' l4 d( l4 g1 A2 B! ~& k6 m

! H3 v' g9 d+ n& t3 o0 S& R' tsub getLog{
' c: b4 K% r8 d7 a8 ~, x/ g7 b, i1 o
  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";* u4 A8 Z# v. u2 [" O0 @
  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  
+ ?' Z) D+ }6 ~) |- l  print $command."\n";; v5 @' u9 I" v* ^; I
    ' J' K$ b8 k2 g8 ^' [
    system($command);
- H5 |& u+ f  i! @7 X  X            - f3 x) w% f- w
    exit();
0 \$ x% Y$ a' O( A}8 I. w! d# O3 p/ @* ?

9 z5 I% _! V: ?5 b: u% O4 Csub Usage {
! d* {% `* k; D6 o$ I# kwarn << "__END_OF_USAGE";
9 `" z/ k6 ]/ g6 |, C% ~" h0 ?* X
9 C' G0 _% f7 Q+ WHoops! you need read the usage!
- C1 S, _1 X9 v+ J/ V$ B/ V3 {        1 E) T  Y- K% T6 Y& e3 @
Usage:
4 A, M, C- k4 m& l8 Y
5 l; F% P$ m6 ~- ~* G6 e3 i  getlog TargetFile StartTime EndTime OutPutFile
. h. Q9 U2 m' e, v8 K  Attention: For 24-hour time-division。
+ }# U: J9 r+ ?/ F- V% n7 t7 _2 P- |
Example:
& ?# D" ~0 l. A2 u  getlog server.log 1530 1620 server_1530_1620.txt
$ R  M3 D" G9 f$ t  (Between the time A and time B)6 Z0 D& x6 h" e8 M/ ]
  .....  i  l2 E" F2 l2 o3 x* ?5 q

! x1 ~+ N9 k" }- \: q0 u0 c5 \__END_OF_USAGE
& s) m8 P" `8 L, G2 S    9 l0 p* J9 G6 ?+ h4 [9 T) C/ w
exit 1;) v" O: h% t. r+ q, G
}7 Z; k! q8 J0 z% t& S

' z) h/ c) w- j: T2 V: r; H5 N5 ]: ?# D5 M: i$ k
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

SCMLife推荐上一条 /4 下一条

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

GMT+8, 2019-4-24 11:08 , Processed in 0.056277 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

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