The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk and regular expression maskot Shell Programming and Scripting 4 05-22-2007 04:22 AM
Regular Expression problem djkane Shell Programming and Scripting 5 06-21-2006 07:07 AM
Regular Expression Problem netmaster UNIX for Dummies Questions & Answers 1 12-07-2005 06:34 PM
Complex Pipeline/Redirection/Regular Expression problem netmaster UNIX for Dummies Questions & Answers 1 11-28-2005 09:55 PM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 07:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-01-2008
Registered User
 

Join Date: Apr 2006
Posts: 74
Regular Expression Problem

this is how my xyz.log file loooks like :-

[30/Dec/2008:12:03:07] info ( 816): CORE1116: Sun ONE Web Server 6.1SP5 B08/17/2005 22:09
[31/Dec/2008:12:03:08] info ( 817): CORE5076: Using [Java HotSpot(TM) Server VM, Version 1.5.0_11] from [Sun Microsystems Inc
.]
[01/Jan/2008:12:03:08] info ( 817): WEB0100: Loading web module in virtual server [http-google] at [/abc]
[02/Jan/2008:12:03:08] info ( 817): WEB0100: Loading web module in virtual server [http-google] at [/abc]


Code:
perl -MPOSIX -i.bak  -ne 'BEGIN{

 $d=strftime("%d%m%Y",localtime time-60*24*60*60);
 @m{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=("01".."12")}

 print if m"^(\d+)/(\w+)/(\d{4})"&& "$1$m{$2}$3" gt $d' xyz.log
I am trying to keep the last 60 days data and delete everything else but for some reason its deleting everything in xyz.log instead of keeping the last 60 days and deleting the rest.

Any info will be really appreciated. I am new to regular expression.
Reply With Quote
Forum Sponsor
  #2  
Old 04-01-2008
Registered User
 

Join Date: Jun 2006
Posts: 164
Perhaps unrelated, but you forgot to escape the forward-slashes in your date in the regex.

Shawn
Reply With Quote
  #3  
Old 04-01-2008
Registered User
 

Join Date: Apr 2006
Posts: 74
Forward slashes missing where?
Reply With Quote
  #4  
Old 04-01-2008
Registered User
 

Join Date: Jun 2006
Posts: 164
Quote:
Originally Posted by chris1234 View Post
Forward slashes missing where?
Sorry, I didn't pay attention to the fact that you're using double-quotes to surround the regex instead of the standard forward-slashes.

Actually, the problem seems to lie with the date formatting.

Code:
 $ perl -MPOSIX -e 'print strftime("%d%m%Y",localtime time-60*24*60*60);'
01022008
The date is in the wrong order, so your gt comparison later will give undesired results. It seems that you have to fix it so that both this date and the format retrieved from the file are re-ordered to YYYYMMDD.

ShawnMilo & Friends (looking over my shoulder preventing me from giving three wrong answers before we found this one.)
Reply With Quote
  #5  
Old 04-01-2008
Registered User
 

Join Date: Apr 2006
Posts: 74
Well re formatiing the file is out of question. Its a log file which is created by a java program. So re formatting the log file is out of question. I modified my script :-

Code:
 perl -MPOSIX -i.bak  -ne 'BEGIN{

 $d=strftime("%Y%m%d",localtime time-60*24*60*60);
 @m{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=("01".."12")}

 print if m"^(\d+)/(\w+)/(\d{4})"&& "$1$m{$2}$3" gt $d' xyz.log
Still not working. here is xyz.log

[30/Dec/2008:12:03:07] info ( 816): CORE1116: Sun ONE Web Server 6.1SP5 B08/17/2005 22:09
[31/Dec/2008:12:03:08] info ( 817): CORE5076: Using [Java HotSpot(TM) Server VM, Version 1.5.0_11] from [Sun Microsystems Inc
.]
Reply With Quote
  #6  
Old 04-01-2008
Registered User
 

Join Date: Jun 2006
Posts: 164
I believe you have to change

Code:
print if m"^(\d+)/(\w+)/(\d{4})"&& "$1$m{$2}$3" gt $d' xyz.log
to

Code:
print if m"^(\d+)/(\w+)/(\d{4})"&& "$3$m{$2}$1" gt $d' xyz.log
(Just swap the $1 and the $3.)

Shawn
Reply With Quote
  #7  
Old 04-01-2008
Registered User
 

Join Date: Apr 2006
Posts: 74
Yea thats what i thought at the first glance and i already tried that swapping 1 to 3 same result. It deletes everything from the file.

Code:
perl -MPOSIX -i -ne 'BEGIN{
 $d=strftime("%Y%m%d",localtime time-60*24*60*60);
 @m{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=("01".."12")}
 print if m"^(\d+)/(\w+)/(\d{4})"&& "$3$m{$2}$1" gt $d' xyz.log
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
perl, perl regex, regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0