SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 3758|回复: 0

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

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑 ' ]7 d6 g* a$ X2 @+ M

: Q0 H0 E2 G4 i5 {0 I& Vsed命令通过关键字截取log。这里将时间转换参与逻辑运行。
/ e* W+ F" ~8 U- \8 x% T提高log提取准确性。1 I% p( ~0 V( v  l$ Q3 J: M/ Y

4 M' P5 G  K: N" o
3 i9 L2 e# F3 b* Y0 t7 r#!/usr/bin/perl+ g2 b  f6 G0 V* S& q

& l8 r$ E( c3 I6 K' p* |. c# Copyright Statement:
4 K3 W' n2 n4 X! j# --------------------
  C2 k( U& F8 F8 U$ g% @# This file is the tool to get the log  X; N, s: V' k. p7 P( X" W3 N
# Between the time A and time B;
8 X# P. q! d$ Z' O$ w9 q( f3 G# Attention:Ensure the system have the sed commond!  n8 f" @: |& ?, t9 ~  F
# @Author: hakisman@gmail.com- h3 I! ]: `4 e$ r
# Date: 2014.2.17#. i" F8 @* N& p* [7 ^
: p; k! H9 z) l: S" K6 O
my $TargetFile = "$ARGV[0]";
7 T  ?! C8 d8 U: \1 amy $StartTime = "$ARGV[1]";
7 f1 c5 l4 X( M4 rmy $EndTime = "$ARGV[2]";9 t( x. ~6 Z8 t8 c) L
my $OutPutFile = "$ARGV[3]";
* V  \5 j& G8 V$ S# R" I6 Y; J, b/ l% H) e% P2 m) ^
if($#ARGV<3){! }( Y  ], {- `; g+ b
    print "ERROR:The number of parameters is wrong!\n";
& {# C# K- w+ r) K" u; u7 i( c, ^9 ^    &Usage;; P$ V- d0 W! d6 M5 q* J, F2 S- s
}, j2 E+ m& ?7 W8 m6 c
if(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){7 }5 @/ }  h6 o% r5 n: a  a
  print "ERROR:The StartTime or Endtime is wrong!\n";& R+ \6 v' C$ n) ]4 u$ ?
  &Usage;4 H* A# m; M3 z' U7 g( e
}
+ w/ B7 ?8 @+ e3 P$ ?& ]# H" I3 ]9 ^/ {. D2 Y) M$ K
my $startLineNumber = 0;
- i9 ^" U& x% {( K. mmy $endLineNumber = 0;0 V- U+ b: d6 _2 g2 v0 H3 A, _
; l) F. M% g, B/ {; C
my $timelocation = 0;! y+ O% j3 V/ t
my $tempString;  e3 C% C3 }# n3 F
8 X7 x" a" _; a8 W$ ^
if(-d $file){* [  l/ X/ L. \, I; R
    print "ERROR:The TargetFile isn't exits!\n";; A0 C3 {$ p: y' T3 O% x: R6 ~
    &Usage;1 U8 H8 U+ d4 o4 _
}else{
( ^& Z9 R- N$ C0 r' U9 S) Z2 g    if(open (readfile,$TargetFile)){2 C6 d% F- L5 z& X# t* z& `
    }else{
/ `. q9 X" _8 Y+ ^      print "ERROR:Read file is faild!\n";( ?( J; z# Z/ N6 ]0 {' }
        #dir ("Open the file faild!\n");
* W6 V9 `$ L  K) a6 \8 L    }. N' @  t# i" K3 E
    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!* e/ \- T% r0 m
    my @FileArray = <readfile>;) d! c8 P) @0 ?: X
   
" Z1 A+ `0 f# d, v: y) @0 N' X    my $filelength = $#FileArray;" j. h9 f" e9 Y8 t- d& F
   
3 W- ?! a% u: }% n    $endLineNumber = $filelength;
+ J1 ?. v- T& M1 _   
; K# A/ w3 J" [5 k. N$ B( z    my $i = 0;
, J' H# H+ ~/ J! h4 e. z2 D6 b; I
0 {, n: e8 `( G) l    while($i<=$filelength){
) O! X- S3 ?- r+ t4 a; C+ T        
9 s+ x2 u1 }; z8 x( @        $tempString = substr($FileArray[$i],10,5);
& Q8 b* H* ~  g+ i        #print $tempString."------\n";5 Y& ^  Q# J9 u& @7 U
        if(($startLineNumber==0)&&(index($tempString,":")==2)){
5 ^* Q9 i3 e8 y6 V4 I, G            $tempString =~ s/\://g;
6 L+ r0 K: P2 x; @$ W" N$ @1 D! ?" K                #print "$tempString\n";
/ i# O! q9 U$ |0 L; L6 V$ w                    if($StartTime<=$tempString){                        
& d6 c$ `3 E% Q) s) H% a9 T                    $startLineNumber=$i;% y1 H+ t; {& n" X+ }7 z
            }/ ?8 V6 _3 @2 M, P4 j! h/ [( j0 h
        }
4 K& g4 y/ t6 o9 e& t$ Z        # f+ d3 K# }& U& e2 ~
        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){& c8 O+ R* x* T& |, }0 u  U7 A
          $tempString =~ s/\://g;
  R$ |4 H: V2 ~3 y          #print "EndTime is $EndTime"."____number1 is $number1\n";3 B" r7 q* j; R
          if($EndTime<=$tempString){
- W" |" a& {' a  j- z! ~0 x              $endLineNumber=$i;   
9 V. u0 c; A8 S0 `. X              &getLog;+ I/ N% o+ ?2 N9 K
          }
& m, v- v, i0 a        }   
: A+ `. `5 W  E3 K; T        $i++;+ W5 s+ U) `$ P+ o- U- k/ t8 Z
    }# L  @1 M: T) n& w
    close(readfile);
& P0 q) V5 v! j, g}
4 W- K$ _7 E+ r" o8 V& S! I: |( f. ^( M; x$ U$ B, t! h

* i9 i( O0 i' @3 X4 Ksub getLog{: Q" b  E9 P, _; f' W2 Q
  o( D$ f  O* \5 p' _
  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";
- h6 d- @" {+ A; `- R" \  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  
" ~: N. H) o1 ]( p0 ^$ m4 M  print $command."\n";
5 Y( A* Q4 {9 L7 c) J$ J! D% E0 t& q    2 E4 \- `$ I3 M2 n+ f( f% H6 X
    system($command);
: B6 L; Y, ~/ N, r* o            ' x* X8 B4 R3 j, B" V
    exit();; Z1 U5 @+ h" `' ?7 E" p) q* N
}
$ l3 |- x' ]0 h6 g
3 `: |5 L7 v- I  S  ?7 Gsub Usage {
) \2 V( d  \' p2 dwarn << "__END_OF_USAGE";6 T" c9 n- }: Y* Y' K
' V2 m# h0 l+ S9 G+ t/ i
Hoops! you need read the usage!
& n$ O7 s4 ~- N% ]        + r! W0 g% g" u6 `# J& p
Usage:4 L0 [- N# w5 u5 b* F: `

/ Q' F& \" w3 R+ U) v8 r# w, \  getlog TargetFile StartTime EndTime OutPutFile  L$ [) W% m, B9 N! S
  Attention: For 24-hour time-division。$ F' d& G' ^5 u

9 o3 t3 D# g' U) _( A: a$ k( mExample:
8 Z( q5 W# V1 ]" R. ~  H- b  getlog server.log 1530 1620 server_1530_1620.txt* I7 T$ ?  F+ T! K2 p
  (Between the time A and time B)
% v3 E1 q( R( d1 V8 ]% Z  .....4 M7 a# n$ v6 P1 G* W* H1 U" h
* S3 C! _' [; a. [
__END_OF_USAGE. y" a4 C+ m7 W& T! z8 q+ a3 M
    ( I& |! s: |9 j+ @1 t0 @6 ]
exit 1;
' ~6 c1 v; {" X9 y: J}( ~- @- L! o1 P3 C  `; ~+ ]! |

9 r8 ~2 g1 b/ {$ \' a& m1 c2 y$ h' W# _% u* u" o! x$ ]6 [5 P7 w
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

SCMLife推荐上一条 /4 下一条

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

GMT+8, 2018-6-22 09:42 , Processed in 0.063164 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

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