grep in file and date process

M

Mr_Noob

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;
 
R

RedGrittyBrick

Mr_Noob said:
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.
 
M

Mr_Noob

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

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

Peter Makholm

Mr_Noob said:
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
 
M

Mr_Noob

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)
 
R

RedGrittyBrick

Mr_Noob said:
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.
 
J

J. Gleixner

Mr_Noob said:
my @clientlog;
open(FILE, "< $xferlog") or die "Can't open $xferlog : $!";
open( my $log, '<', $xferlog) or die "...";
while(<FILE>)
while ( said:
{
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

J. Gleixner

J. Gleixner said:
Mr_Noob said:
my @clientlog;
open(FILE, "< $xferlog") or die "Can't open $xferlog : $!";
open( my $log, '<', $xferlog) or die "...";
while(<FILE>)
while ( said:
{
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]; }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top