Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > grep in file and date process

Reply
Thread Tools

grep in file and date process

 
 
Mr_Noob
Guest
Posts: n/a
 
      03-03-2008
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;

 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      03-03-2008
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;
>


 
Reply With Quote
 
 
 
 
Mr_Noob
Guest
Posts: n/a
 
      03-03-2008
On Mar 3, 1:49 pm, RedGrittyBrick <(E-Mail Removed)>
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 ?
 
Reply With Quote
 
Peter Makholm
Guest
Posts: n/a
 
      03-03-2008
Mr_Noob <(E-Mail Removed)> 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
 
Reply With Quote
 
Mr_Noob
Guest
Posts: n/a
 
      03-03-2008
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)
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      03-03-2008
Mr_Noob wrote:
> On Mar 3, 1:49 pm, RedGrittyBrick <(E-Mail Removed)>
> 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.
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      03-04-2008
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.
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      03-04-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ps -ef | grep "some running process" puja sonwane Software 0 04-01-2009 01:47 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments