SCMLife.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 3147|回复: 0

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

[复制链接]
发表于 2014-9-1 16:07:39 | 显示全部楼层 |阅读模式
本帖最后由 quake 于 2014-9-1 16:08 编辑
5 D3 M! q, r! L) u3 U% s: \4 ~" ?% n/ Z4 I0 U; L
sed命令通过关键字截取log。这里将时间转换参与逻辑运行。8 C: ~' o) I5 Z- Y; m% D
提高log提取准确性。
& E# r% p: i6 X, D5 G$ L$ Q# x! o; R( F; P) z* p' P' r3 p

, N/ b# L- E7 L# N+ F( o  |* k* D#!/usr/bin/perl
$ |% i0 z% k8 p6 l+ j
) E) v* B8 F# P5 v, o2 [, E# Copyright Statement:3 R' e6 R3 }. k
# --------------------
; ]+ M, e8 X- A0 a# d# This file is the tool to get the log. R9 E6 S5 k3 Y' N4 v. ]; E
# Between the time A and time B;
2 F4 ], N4 Z2 h0 F8 U! y" I# Attention:Ensure the system have the sed commond!
' x% V# Y8 I" _, v+ U& @# @Author: hakisman@gmail.com8 N) d4 P9 Q2 N: v
# Date: 2014.2.17#
0 v+ S1 m( O5 ?" I. q9 D7 c/ Q( U4 T, S9 ~" d$ x9 g( ^
my $TargetFile = "$ARGV[0]"; . t! H) |8 c% u. {
my $StartTime = "$ARGV[1]";
* l  A) Q: d& y5 L- smy $EndTime = "$ARGV[2]";
- F' v- }* v5 L. Z' S) umy $OutPutFile = "$ARGV[3]";7 E. k* `$ |3 o

/ G; _: h0 }/ |$ L1 g2 r/ W- `if($#ARGV<3){3 v, J* o* W% C- k5 K
    print "ERROR:The number of parameters is wrong!\n";) K/ a/ {. b4 K7 v
    &Usage;0 H3 }; E, A5 V, P2 Y+ c
}
; O$ i/ S4 D8 D* U* h* x# pif(($StartTime =~/[^0-9]+/)||($EndTime =~/[^0-9]+/)||($EndTime<=$StartTime)){
, Q$ s, E4 D) e4 G  print "ERROR:The StartTime or Endtime is wrong!\n";
+ m+ M5 }2 ~* T  &Usage;
8 D- P8 R) I; I}! {- v2 X% k  x$ K- T+ D) \
9 @7 t3 U2 D/ T8 B/ i
my $startLineNumber = 0;7 T' I: W$ C6 c
my $endLineNumber = 0;+ Y9 D3 G6 C4 r. l

# A4 \' U( q, u/ H9 Zmy $timelocation = 0;
& b+ f7 [. b8 n  V1 xmy $tempString;
1 A+ D8 r! ^5 A0 P) g& |, n/ T
if(-d $file){9 i; O) V. @  R. l4 U
    print "ERROR:The TargetFile isn't exits!\n";
  y( S3 R; g6 S! s7 W3 F4 y, q- e    &Usage;3 O8 R" D' \2 W
}else{$ Z4 L" F. _; u# y  U. g7 D
    if(open (readfile,$TargetFile)){
8 o( O: f5 E% p: [    }else{: Q& b- O3 Y; y% ]0 ^
      print "ERROR:Read file is faild!\n";
  j& z$ _9 Q  U: R; w2 n        #dir ("Open the file faild!\n");
: d( I' X0 N! p5 v& |: }# k. U5 x, h    }
/ j% U- Q8 E- `2 v6 C    #Waring this place put all the file into the Memroy.mybe cause the out of memroy!9 X8 ]& @7 d0 j. s% h
    my @FileArray = <readfile>;
$ ]5 p6 I7 |* l7 ?+ l   
8 @, E: c7 f" b6 k0 k1 Y" Y    my $filelength = $#FileArray;* t: H- a: }: |1 h( s
    ! w" O) m+ S2 j; B" ]( _3 U
    $endLineNumber = $filelength;
, N' s& m  w9 H8 q3 o5 S    % w$ H# d+ u& E( w2 Q; U! {
    my $i = 0;
) D, [3 E* E( Q2 h5 K
4 _3 ]2 z# h  ?8 u7 F    while($i<=$filelength){
5 O$ A0 s4 Z/ J0 `8 a- r) m, c        
' k4 X, v* M/ `" @. {        $tempString = substr($FileArray[$i],10,5);
" B, P6 n. v: X+ z3 X        #print $tempString."------\n";
! N" @7 L1 {! ^4 _5 O        if(($startLineNumber==0)&&(index($tempString,":")==2)){+ g) @4 R6 a5 W) ?" L( E) x. _
            $tempString =~ s/\://g;+ U% \5 k/ z+ t0 w
                #print "$tempString\n";
3 d: ~. L' r! z2 R" g                    if($StartTime<=$tempString){                        / _0 d+ K5 {4 h/ P! i4 J+ Y! y
                    $startLineNumber=$i;
# Q# Y1 }- P, `5 S4 }- d2 w            }
4 O  K+ {  C9 g0 V        }
8 }2 t# s; z  R6 P6 F- h4 ^. D* [        
  d/ q4 O# B! A9 a( z$ W        if(($startLineNumber!=0)&&(index($tempString,":")==2)&&($endLineNumber==$filelength)){# }+ I$ F) |5 s- A; _
          $tempString =~ s/\://g;( f, L6 q5 p3 J
          #print "EndTime is $EndTime"."____number1 is $number1\n";  P& Q0 n* ^+ q6 p
          if($EndTime<=$tempString){! p9 ^; Y; p3 Y4 b- Z+ `: d9 N
              $endLineNumber=$i;    1 u& v$ v+ x; P: r6 l$ p  @+ {
              &getLog;
! h  E+ ^8 S) @1 ]9 u5 _          }! T1 B7 j; S3 V# o6 @
        }   
" f. v# P6 n5 c6 R% n7 E4 W        $i++;, M9 V% x( ^* U5 n+ ~
    }
. x8 s6 l* r+ A    close(readfile);
% p7 y  s+ T4 n( }, j7 M+ a# m% o# ?}% p: D" t: z' o

/ B* E7 a0 N8 E6 a9 V' \' S8 u) M& _/ i% f+ S! g
sub getLog{
* Z: G# j2 y$ U3 \8 L: D0 v9 A" ~9 z1 k/ Z
  print "The log text is between the ".$startLineNumber." and ".$endLineNumber.";\n";
9 ~  p) I. T0 y+ Y/ ?+ w/ F* d  my $command = "sed -n '".$startLineNumber.",".$endLineNumber."p' $TargetFile>".$OutPutFile;  
, n0 P" R1 v8 n2 ]2 e  print $command."\n";
  d/ V( h( O& ]8 w2 @$ X   
; u6 ~  e8 \% F" V    system($command);) D9 o1 r" X/ Q; V+ |
            
0 k( |% f+ g) y0 u2 q: f1 E& u    exit();
+ B- G0 l+ r  y7 ?}2 J0 t: u" ?6 O8 q* q+ C
% x8 K0 m9 q: }2 L* n7 b1 t( S$ P6 H
sub Usage {
8 I. ^, G% C9 d, ^8 j. Jwarn << "__END_OF_USAGE";- B0 k* ?: \/ L( M: \
! |0 u. Z# A8 v( X5 g
Hoops! you need read the usage!
: e! v8 x  W4 K% @8 m: ?) l; s( ]        
* `* ^5 H& ?% z0 y' a; `9 l# w7 g+ v- o9 `Usage:
+ Z$ g9 m2 n6 f
1 ?0 t" f  p  t7 f; ^* g# Z  getlog TargetFile StartTime EndTime OutPutFile
/ U7 V7 n# r8 I$ E5 E/ _! x3 Z" {  Attention: For 24-hour time-division。
  E6 |, m# ~) x! z( ?) E# _  h$ M9 [3 n% f3 f( K/ \
Example:+ {5 R; P7 F1 K' ], B4 v$ ]
  getlog server.log 1530 1620 server_1530_1620.txt, p- o4 {- n; V# A$ I( s. s
  (Between the time A and time B)
. p: g! r+ t9 a7 p, j  .....* h" Q9 D8 P) }; t, A+ d, r1 c; d

- n% N+ ^4 p: T& U__END_OF_USAGE! r* f' g4 i4 s0 u2 _9 v
   
1 Z0 B' G7 u) T& {( D; i/ S* \exit 1;. h3 A' J! K2 h' h: ?- l, r. ^3 Y
}
/ q: ]/ r; A, B6 o
/ ~, j' Q" g- k$ j$ D' t
9 E( D" X+ R" f+ {! {6 e0 `
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2018-1-17 22:20 , Processed in 0.063100 second(s), 6 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

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