Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find newest file in directory and then search it

Posted on 2004-04-15
10
Medium Priority
?
768 Views
Last Modified: 2007-12-19
I have a batch job that i run about 3 to 4 times a day. It creates a log of format 1262002-1000 am.log i want to find the newest log file and then grep its results. I have the grep part written but am struggling with how to do the opendir and use a array to find the newest file and then run my grep against that.
0
Comment
Question by:gdmacmillan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 7

Expert Comment

by:fim32
ID: 10836320
why use perl?

if you already have your grep:

grep "searchstring" `ls -tr | tail -1`

tho, if you wanted, you could put that backtick command into perl, too...
0
 

Author Comment

by:gdmacmillan
ID: 10836341
Sorry forgot to mention im doing this on a windows system thats why im using perl.
0
 
LVL 20

Accepted Solution

by:
jmcg earned 1760 total points
ID: 10836357
We have been asked something very much like this before:

http://www.experts-exchange.com/Q_20845254.html

For your case, I think it might work if you started out with something like the following:

use strict;
use File::stat;

my $newest = [ undef, 0 ];

for ( <*.log> ) {
    my $st = stat($_) or next; # should warn here?
    my $mtime = $st->mtime;

    $newest = [ $_, $mtime] if $mtime > newest->[1] ;
    }

print "most recent file is: ", $newest->[0], "\n";

This should be reasonably efficient and I think I've avoided the typos that plagued that other question. With $newest->[0] in hand as your filename, you can then apply whatever grep code you had already worked out.



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:fim32
ID: 10836386
ugh.

and your log name leaves a little to be desired... is your example from jan 26th? or from dec 6th?

you could scan the filenames into an array and use sort, but only after you did some magic on the names. specifically, you need to alter all the filenames so they can be compared... like using YYYYMMDDHHMM.log instead of what you got with the am's and all...
0
 
LVL 7

Expert Comment

by:fim32
ID: 10836389
or, use File::stat as jmcg suggests :P
0
 

Author Comment

by:gdmacmillan
ID: 10836669
Hi Jcmp,
          yep ive been looking at those other awnsers but i am quite a newbie to perl. The last programming i did was pascal back in college in 90.  I understand we are creating a array which is undefined and then searching through the files to find the newest.  The stat function is used to get info from the flie. What i get stuck at is how do i tell it which directory to go search.
0
 
LVL 7

Assisted Solution

by:fim32
fim32 earned 240 total points
ID: 10837127
that's in the for part:

for ( <.*log> ) {

the <.*log> part says search the local directory for all files names .*log, it's a shortcut to using the opendir and such.
0
 

Author Comment

by:gdmacmillan
ID: 10837161
Doh. So if i run it from that directory it will search that directory? What if i wanted to use it to go search another one?
0
 
LVL 7

Expert Comment

by:fim32
ID: 10837330
you could as easily wrap that for loop in a opendir/closedir:

opendir D,"/newdir";
for (readdir D) {
  other stuffs here
}
closedir D;

or, if you wanted to make sure it was only *log files you were getting, change the abovre for (readdir) to:
for (grep { /.*log/ } readdir D) {
0
 

Author Comment

by:gdmacmillan
ID: 10843632
Hi fim32 & Jmcg thanks for all your help on this. I have the program working now.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question