grep in file and date process

Discussion in 'Perl Misc' started by Mr_Noob, Mar 3, 2008.

  1. Mr_Noob

    Mr_Noob Guest

    Hi all,

    here is an extract of my file :

    2008/03/03 12:56:07 put /name_of_client4/pub/file.xml
    2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.mov
    2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.wmv
    2008/03/03 12:56:07 put /name_of_client5/pub/file.xml
    2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.flv
    2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.jpg
    2008/03/03 12:56:07 put /name_of_client2/pub/file.jpg
    2008/03/03 12:56:07 put /name_of_client3/pub/file.xml
    2008/03/03 12:56:07 put /name_of_client5/pub/file.mov
    2008/03/03 12:56:07 put /name_of_client1/pub/file.xml
    ....

    What would be the best way to grep for a specified "name_of_client"
    then calculate the time since last file has been put ?

    open( FILE, "< $filename" ) or die "Can't open $filename : $!";
    while( <FILE> ) {
    # grep last appearance of specified name_of_client
    # time since last transfer
    }
    close FILE;
    Mr_Noob, Mar 3, 2008
    #1
    1. Advertising

  2. Mr_Noob wrote:
    > Hi all,
    >
    > here is an extract of my file :
    >
    > 2008/03/03 12:56:07 put /name_of_client4/pub/file.xml
    > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.mov
    > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.wmv
    > 2008/03/03 12:56:07 put /name_of_client5/pub/file.xml
    > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.flv
    > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.jpg
    > 2008/03/03 12:56:07 put /name_of_client2/pub/file.jpg
    > 2008/03/03 12:56:07 put /name_of_client3/pub/file.xml
    > 2008/03/03 12:56:07 put /name_of_client5/pub/file.mov
    > 2008/03/03 12:56:07 put /name_of_client1/pub/file.xml
    > ...
    >
    > What would be the best way to grep for a specified "name_of_client"
    > then calculate the time since last file has been put ?
    >
    > open( FILE, "< $filename" ) or die "Can't open $filename : $!";
    > while( <FILE> ) {
    > # grep last appearance of specified name_of_client
    > # time since last transfer


    Some combination of 'next unless /name_of_client/', split, a hash and a
    date module.

    > }
    > close FILE;
    >
    RedGrittyBrick, Mar 3, 2008
    #2
    1. Advertising

  3. Mr_Noob

    Mr_Noob Guest

    On Mar 3, 1:49 pm, RedGrittyBrick <>
    wrote:
    > Mr_Noob wrote:
    > > Hi all,

    >
    > > here is an extract of my file :

    >
    > > 2008/03/03 12:56:07 put /name_of_client4/pub/file.xml
    > > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.mov
    > > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.wmv
    > > 2008/03/03 12:56:07 put /name_of_client5/pub/file.xml
    > > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.flv
    > > 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.jpg
    > > 2008/03/03 12:56:07 put /name_of_client2/pub/file.jpg
    > > 2008/03/03 12:56:07 put /name_of_client3/pub/file.xml
    > > 2008/03/03 12:56:07 put /name_of_client5/pub/file.mov
    > > 2008/03/03 12:56:07 put /name_of_client1/pub/file.xml
    > > ...

    >
    > > What would be the best way to grep for a specified "name_of_client"
    > > then calculate the time since last file has been put ?

    >
    > > open( FILE, "< $filename" ) or die "Can't open $filename : $!";
    > > while( <FILE> ) {
    > > # grep last appearance of specified name_of_client
    > > # time since last transfer

    >
    > Some combination of 'next unless /name_of_client/', split, a hash and a
    > date module.
    >
    > > }
    > > close FILE;


    ok , but how can I tell my script to grep only the last appearance of
    a client's name for further process ?
    Mr_Noob, Mar 3, 2008
    #3
  4. Mr_Noob <> writes:

    >> > open( FILE, "< $filename" ) or die "Can't open $filename : $!";
    >> > while( <FILE> ) {
    >> > # grep last appearance of specified name_of_client
    >> > # time since last transfer

    >>
    >> Some combination of 'next unless /name_of_client/', split, a hash and a
    >> date module.
    >>
    >> > }
    >> > close FILE;

    >
    > ok , but how can I tell my script to grep only the last appearance of
    > a client's name for further process ?


    Moving the further processing outside the loop might do it...

    //Makholm
    Peter Makholm, Mar 3, 2008
    #4
  5. Mr_Noob

    Mr_Noob Guest

    my @clientlog;
    open(FILE, "< $xferlog") or die "Can't open $xferlog : $!";
    while(<FILE>)
    {
    next unless /put \/$client/;
    push (@clientlog, $_);

    }
    # Display last element
    my $lastclientlog = $clientlog[$#clientlog];
    print $lastclientlog;
    close FILE;


    This works great but i cannot get rid of this error in apache's log
    file:

    "Use of uninitialized value in print..." (line 10)
    Mr_Noob, Mar 3, 2008
    #5
  6. Mr_Noob wrote:
    > On Mar 3, 1:49 pm, RedGrittyBrick <>
    > wrote:
    >> Mr_Noob wrote:
    >>> Hi all,
    >>> here is an extract of my file :
    >>> 2008/03/03 12:56:07 put /name_of_client4/pub/file.xml
    >>> 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.mov
    >>> 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.wmv
    >>> 2008/03/03 12:56:07 put /name_of_client5/pub/file.xml
    >>> 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.flv
    >>> 2008/03/03 12:56:07 put /name_of_client1/pub/videos/file.jpg
    >>> 2008/03/03 12:56:07 put /name_of_client2/pub/file.jpg
    >>> 2008/03/03 12:56:07 put /name_of_client3/pub/file.xml
    >>> 2008/03/03 12:56:07 put /name_of_client5/pub/file.mov
    >>> 2008/03/03 12:56:07 put /name_of_client1/pub/file.xml
    >>> ...
    >>> What would be the best way to grep for a specified "name_of_client"
    >>> then calculate the time since last file has been put ?
    >>> open( FILE, "< $filename" ) or die "Can't open $filename : $!";
    >>> while( <FILE> ) {
    >>> # grep last appearance of specified name_of_client
    >>> # time since last transfer

    >> Some combination of 'next unless /name_of_client/', split, a hash and a
    >> date module.
    >>
    >>> }
    >>> close FILE;

    >
    > ok , but how can I tell my script to grep only the last appearance of
    > a client's name for further process ?


    Don't, just overwrite the hash (e.g. $lastdate{$client}=$date;). After
    closing the file, loop through the hash and do there whatever processing
    you had envisaged doing in your while(<FILE>) loop.
    RedGrittyBrick, Mar 3, 2008
    #6
  7. Mr_Noob

    J. Gleixner Guest

    Mr_Noob wrote:
    > my @clientlog;
    > open(FILE, "< $xferlog") or die "Can't open $xferlog : $!";

    open( my $log, '<', $xferlog) or die "...";
    > while(<FILE>)

    while ( <$log> )
    > {
    > next unless /put \/$client/;
    > push (@clientlog, $_);
    >
    > }
    > # Display last element
    > my $lastclientlog = $clientlog[$#clientlog];
    > print $lastclientlog;
    > close FILE;


    close $log;
    if ( @clientlog ) { print $clientlog[-1], "\n"; }
    else { print "Didn't find anything in $xferlog for $client.\n"; }

    >
    >
    > This works great but i cannot get rid of this error in apache's log
    > file:
    >
    > "Use of uninitialized value in print..." (line 10)


    Your definition of 'works great' is a little off. :)
    J. Gleixner, Mar 4, 2008
    #7
  8. Mr_Noob

    J. Gleixner Guest

    J. Gleixner wrote:
    > Mr_Noob wrote:
    >> my @clientlog;
    >> open(FILE, "< $xferlog") or die "Can't open $xferlog : $!";

    > open( my $log, '<', $xferlog) or die "...";
    >> while(<FILE>)

    > while ( <$log> )
    >> {
    >> next unless /put \/$client/;
    >> push (@clientlog, $_);
    >>
    >> }
    >> # Display last element
    >> my $lastclientlog = $clientlog[$#clientlog];
    >> print $lastclientlog;
    >> close FILE;

    >
    > close $log;
    > if ( @clientlog ) { print $clientlog[-1], "\n"; }


    Slight correction.. Since you didn't chomp(), you don't
    need the newline.

    if ( @clientlog ) { print $clientlog[-1]; }

    > else { print "Didn't find anything in $xferlog for $client.\n"; }
    >
    >>
    >>
    >> This works great but i cannot get rid of this error in apache's log
    >> file:
    >>
    >> "Use of uninitialized value in print..." (line 10)

    >
    > Your definition of 'works great' is a little off. :)
    J. Gleixner, Mar 4, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    595
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,244
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    514
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    718
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    404
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page