[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Grep a file  by date  in Perl

Posted on 2011-10-03
5
Medium Priority
?
413 Views
Last Modified: 2012-08-14
Hi,
I have a path "/usr/home/Test/Files" which contains a bunch of .txt files. The name format of these files are :

/usr/home/Test/Files:

test_20111003_hdl.txt.  
test_20111002_hdl.txt
test_20111001_hdl.txt
test_20110930_hdl.txt..

I want to grep the latest file by the date concatenated in the name of the file.  Can any one pls let me know how.

Thanks,
0
Comment
Question by:new_perl_user
  • 2
  • 2
5 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 36907260
use POSIX;
$todaysfilename=strftime"/usr/home/Test/Files/test_%Y%m%d_hdl.txt",localtime;
0
 
LVL 9

Expert Comment

by:parparov
ID: 36907291
You need the latest file, or the files past a certain date?
0
 

Author Comment

by:new_perl_user
ID: 36907304
it should grep latest file.
0
 
LVL 9

Accepted Solution

by:
parparov earned 2000 total points
ID: 36907313
latest file would be:
my $todaysfile = pop sort (grep /test_.*_hdl.txt$/, readdir DIR);

Open in new window

0
 
LVL 85

Expert Comment

by:ozo
ID: 36907357
for the file that was modified most recently, regardless of the name:

($latestfile)=(sort map{sprintf"%9x\0%s",(stat)[9]*10,$_}</usr/home/Test/Files/*.txt>)[-1]=~/\0(.*)/
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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
Suggested Courses

867 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