HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   Capture a log portion closest of 2 hours (http://www.howtoforge.com/forums/showthread.php?t=60557)

bejaia 14th February 2013 06:41

Capture a log portion closest of 2 hours
 
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:

cat server.log
Code:

error jonas
aaaaaaaaaaaaaa
bbbbbbbbbbbbb
cccccccccccc
24/01/2013 09:10 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'



All times are GMT +2. The time now is 19:24.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.