Problem with perl pattern for date


 
Thread Tools Search this Thread
Top Forums Programming Problem with perl pattern for date
# 1  
Old 02-25-2011
Problem with perl pattern for date

Hello Friends,

I have been struck with a perl script for quite some time now. I have a log file which gives a date which is sometimes coming in this format

Code:
Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 15:12:50

or sometimes in this format

Code:
Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 
15:12:50

where the date is going to the 2nd line.

I am able to do a search for the first pattern like this

Code:
 
if($_ =~ /invoked by root at (\d+)\/(\d+)\/(\d+)[\s|\r|\n](\d+):(\d+):(\d+){1}/)

Pls help me out what to do if the pattern is in the 2nd format.

Last edited by achak01; 02-25-2011 at 09:18 PM.. Reason: To depict the problem more accurately
# 2  
Old 02-26-2011
Quote:
Originally Posted by achak01
... what to do if the pattern is in the 2nd format.
Code:
$
$ cat -n logfile
     1  this is the first line
     2  this is the second line
     3  Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 15:12:50
     4  this is the fourth line
     5  Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/26/11
     6  21:45:39
     7  this is the seventh line
$
$
$ perl -lne 'BEGIN {undef $/} print $1 while (/\n(Script.*?invoked by root at (\d+)\/(\d+)\/(\d+).*?(\d+):(\d+):(\d+))/msg)' logfile
Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 15:12:50
Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/26/11
21:45:39
$
$

tyler_durden
# 3  
Old 02-26-2011
The code doesnt seem to work. Actually i have another similar pattern at the bottom of the fiole which is this format

HPOM maintenance summary: 02/26/11 15:39:58. So this search pattern which u specified is matching this one instead of the first one which i specified .

---------- Post updated at 08:37 PM ---------- Previous update was at 02:36 AM ----------

I have made the breakthroufg. here is the code snippet

Code:
open(INFILE,"install.log")|| die("File not found");

while($line = <INFILE>)

{
    if ($line =~ /((\d+)\/(\d+)\/(\d+))\Z/g)
    
    {
        $match = $&;
        
        $nextline = <INFILE>;
        
        
        $start="$match$nextline";

        if($start =~ /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+)(\d+)/)
        {
            print $4,"\n";
        }
    } # end if  if
    
    elsif ($line =~ /invoked by root at ((\d+)\/(\d+)\/(\d+))\s+(\d+):(\d+):(\d+)\Z/)
    
    {
        print  $&;
    }
 } # //end if while

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in passing date to external function from perl script.

my $sysdate = strftime('%Y-%m-%d', localtime ); biDeriveByDate('Table_Str',$sysdate,\@lIndx,\@lResVals) In a perl script, when I'm trying to pass $sysdate to some external function it's not working since $sysdate is passed as a string mentioned above but my function is expecting a date value... (1 Reply)
Discussion started by: Devesh5683
1 Replies

2. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

3. Shell Programming and Scripting

Problem with date in perl!!

Hi All, I am facing an issue with perl.. I have a perl script that executes the stored procedure and puts the data in a file. In stord proc we have one date column also. In table from which it is fetching the data, date is in the form "14/03/2010 00:00:00.000" (DD/MM/YYYY). But when the perl... (4 Replies)
Discussion started by: abhisharma23
4 Replies

4. Shell Programming and Scripting

problem with date in perl

hi guys, i have a variable date that stores the current date....now i need to subtract 2 minutes from it...can someone tell me how to do it in perl.... (5 Replies)
Discussion started by: niteesh_!7
5 Replies

5. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

6. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

7. Shell Programming and Scripting

Date problem in perl

Hi, My script is here-- #!/usr/bin/perl no warnings "uninitialized"; use File::Copy; use File::stat; use Time::Local; use IO::Handle; use DateTime; use Getopt::Long; use File::Glob ':glob'; my $Summary; my $Individual; my $Diagnostics; my $All; (3 Replies)
Discussion started by: namishtiwari
3 Replies

8. Shell Programming and Scripting

Pattern matching problem in PERL script

Hi Friends, As my old friends knows, I'm old to shell script but very new to perl script, currently I'm writing a PERL script with the following functionality: I've multiple product directories, like BUSS, FIN, SALES, MKT etc., : /export/home/GLK/BUSS, /export/home/GLK/FIN, ... (11 Replies)
Discussion started by: ganapati
11 Replies
Login or Register to Ask a Question