Regular Expression Problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Regular Expression Problem
# 1  
Old 04-01-2008
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.
# 2  
Old 04-01-2008
Perhaps unrelated, but you forgot to escape the forward-slashes in your date in the regex.

Shawn
# 3  
Old 04-01-2008
Forward slashes missing where?
# 4  
Old 04-01-2008
Quote:
Originally Posted by chris1234
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.)
# 5  
Old 04-01-2008
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
.]
# 6  
Old 04-01-2008
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
# 7  
Old 04-01-2008
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with Regular expression in awk

Hi, I have a file with two fields in it as shown below 14,30 28,30 16,30 22,30 21,30 3,30 Fields are separated by comma ",". I've been trying to validate the file based on the condition "each field must be a numeric value" I am using HP-UX OS. I have tried the following awk... (4 Replies)
Discussion started by: meetsriharsha
4 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

Regular Expression problem

I have two input files (given below) and to compare each line of the File1 with each line of File2 starts with '>sample1'. If a match occurs and that matched line in the File2 contains another line or sequence of lines starting with "Chr" they have to be displayed in output file with that sample.... (8 Replies)
Discussion started by: hravisankar
8 Replies

4. Shell Programming and Scripting

SED (regular expression) problem ---

Hello, I would like to replace Line 187 of my file named run_example. The original line is below, including the spaces: celldm(1) = 6.00, I want it to become something like celldm(1) = 6.05, or celldm(1) = 6.10, where the number is stored in a variable called... (6 Replies)
Discussion started by: bluesmodular
6 Replies

5. Shell Programming and Scripting

Problem with regular expression

Witam, mam oto taki ciąg znaków: 8275610268 + 9012383215 =niepotrzebnytextPotrzebuję w bash'u wyciągnąć obie liczby (mają taką samą liczbę cyfr), zapisać je do osobnych zmiennych, ale coś nie idzie, kombinowałem z grepem, ale nie potrafię skleić tego wyrażenia regularnego, no i potem przypisać do... (7 Replies)
Discussion started by: menda90
7 Replies

6. Shell Programming and Scripting

problem with Regular expression as input in shell script

Hi, I have script which will take a string as input and search in a file. But when I want to search a pattern which has special characters script is ignoring it. For example: I want to search a pattern "\.tumblr\.com". shell script is removing \ (backslah) and trying to search... (7 Replies)
Discussion started by: Anjan1
7 Replies

7. Shell Programming and Scripting

Problem with a regular expression

Hello! I'm working with AWK, and i have this code: /<LOOP_TIME>/,/<\/LOOP_TIME>/ I want that match every everything between <LOOP_TIME> and </LOOP_TIME>, but not if the line have a "#" before the tags. Someone can help me? Thanks! (6 Replies)
Discussion started by: claw82
6 Replies

8. Shell Programming and Scripting

New line problem of regular expression

could anybody tell me how i can add/append a new line using regular expression in vi on AIX? i've tried several ways before, but all of them failed. e.g. :%s/$/\n/ :%s/^/\v\r/ :( (1 Reply)
Discussion started by: wrl
1 Replies

9. Shell Programming and Scripting

Regular Expression problem

Hi guys I've been trying to write a regular expression. If I'm tryin to validate a sequence of characters as follows... AB1-232-623482-743 43/3 where a) any character after the "AB" can be any alphanumeric character b) the " 43/3" part is optional is there a quick neat way for me... (5 Replies)
Discussion started by: djkane
5 Replies

10. UNIX for Dummies Questions & Answers

Regular Expression Problem

Display all of the lines in a file that contain "Raspberry" followed later in the line by the letter "a" I tried: grep Raspberry*a filename that didn't work Anyone know a solution? (1 Reply)
Discussion started by: netmaster
1 Replies
Login or Register to Ask a Question