Capture a log portion closest of 2 hours

Discussion in 'Programming/Scripts' started by bejaia, Feb 14, 2013.

  1. bejaia

    bejaia New Member

    Hello,

    ./my_script.sh works without brackets.
    I want to modify ./my_script.sh when in the server.log the date and hour are between brackets like that [24/01/2013 09:10]
    I tried in vain certains modifications.
    Can you help me ? Thank you for your help.


    Code:
    [COLOR="Sienna"]cat server.log[/COLOR]
    Code:
    error jonas
    aaaaaaaaaaaaaa
    bbbbbbbbbbbbb
    cccccccccccc
    [COLOR="Green"]24/01/2013 09:10[/COLOR] sssssssss
    sssssssssssssss
    cccccccccccccc
    nnnnnnnnnnnnn
    24/01/2013 10:10 uuuuuuuuu
    uuuuuuuuuuuuuuu
    jjjjjjjjjjjjjj
    ttttttttttttttt
    mmmmmmmmmmmmm
    24/01/2013 10:30 oooooooooo
    oooooooooooo
    sssssssssssss
    qqqqqqqqqqq
    24/01/2013 10:45 vvvvvvvvv
    vvvvvvvvv
    sssssssss
    wwwwwwwwww 
    
    my_script.sh

    Code:
    file=$1
    if [ -f $file.gz ]
         then command="gunzip -c"
         else command=cat
    fi
    $command $file | awk -F"[/ ]" -v S="24/01/2013 10:10" -v E="24/01/2013 10:30" '
    function dcmp(b) {
      if($3>b[3])return  1;
      if($3<b[3])return -1;
      if($2>b[2])return  1;
      if($2<b[2])return -1;
      if($1>b[1])return  1;
      if($1<b[1])return -1;
      if($4>b[4])return  1;
      if($4<b[4])return -1;
      return 0;
    }
    BEGIN{split(S, ds, "[/ ]"); split(E, de, "[/ ]") }
    /^[0-9][0-9]\/[0-1][0-9]\/[0-9][0-9][0-9][0-9] / {
       if(s&&dcmp(de)>=0) {print; exit}
       if(!s&&dcmp(ds)<=0) {f=x;w=1}
       if(!s&&dcmp(ds)>=0) {printf "%s",f; f=x; s=1 }
    }
    !w&&!s {f=f $0 "\n"}
    s'
     

Share This Page