smovva1
asked on
readdir and grep
In perl, I have 2 variables. One is the customer name and another is the date. And exclude . and ..
I need to get the files that match this criteria.
I am not sure how to use grep to do this..
my ($sec,$min,$hour,$mday,$mo n,$year,$w day,$yday, $isdst) = localtime(time);
my $yesterday = sprintf("%04d%02d%02d", $year+1900, $mon, $mday-1);
my $customer = "abc";
@files = grep /$yesterday/, readdir(DIR);
print "File count = @files\n";
I need to get the files that match this criteria.
I am not sure how to use grep to do this..
my ($sec,$min,$hour,$mday,$mo
my $yesterday = sprintf("%04d%02d%02d", $year+1900, $mon, $mday-1);
my $customer = "abc";
@files = grep /$yesterday/, readdir(DIR);
print "File count = @files\n";
ASKER
I don't want to change dir because I have to do this in many places and sometimes within an other directory.
@files = grep /$yesterday/, readdir(DIR); doesn't work.
If I explicetly set it to the date for example, @files = grep {/20120628/} readdir(DIR); it works.
What I want is to get this to work.
@files = grep {/$customer/ and /$yesterday/ } readdir(DIR);
I am looking for the correct syntax for this.
@files = grep /$yesterday/, readdir(DIR); doesn't work.
If I explicetly set it to the date for example, @files = grep {/20120628/} readdir(DIR); it works.
What I want is to get this to work.
@files = grep {/$customer/ and /$yesterday/ } readdir(DIR);
I am looking for the correct syntax for this.
without chdir
@files = <path/to/directory/*$yeste rday*>;
In what way does
@files = grep /$yesterday/, readdir(DIR);
or
@files = grep {/$customer/ and /$yesterday/ } readdir(DIR);
not work?
It should find filenames which contain both "abc" and "20120528" (which is last month)
but sprintf("%04d%02d%02d", $year+1900, $mon, $mday-1); won't be a valid date in January or on the first day of a month.
@files = <path/to/directory/*$yeste
In what way does
@files = grep /$yesterday/, readdir(DIR);
or
@files = grep {/$customer/ and /$yesterday/ } readdir(DIR);
not work?
It should find filenames which contain both "abc" and "20120528" (which is last month)
but sprintf("%04d%02d%02d", $year+1900, $mon, $mday-1); won't be a valid date in January or on the first day of a month.
ASKER
I like @files = <path/to/directory/*$yeste rday*>; Can you do recursive to go into sub-directories?
Thats a good point about the sprintf. What's the best way to get previous day's date in that format.
I will bump up the points.
Thats a good point about the sprintf. What's the best way to get previous day's date in that format.
I will bump up the points.
If you don't care about being wrong for two hours per year near midnight after a daylight saving switchover happens, you might use
my ($mday,$mon,$year) = (localtime time-24*60*60)[3..5];
my $yesterday = sprintf("%04d%02d%02d", $year+1900, $mon+1, $mday);
If you do care about being accurate near midnight after a daylight saving switch we can add a little more code to handle that case.
To do recursive, it may be easiest to
use File::Find;
my ($mday,$mon,$year) = (localtime time-24*60*60)[3..5];
my $yesterday = sprintf("%04d%02d%02d", $year+1900, $mon+1, $mday);
If you do care about being accurate near midnight after a daylight saving switch we can add a little more code to handle that case.
To do recursive, it may be easiest to
use File::Find;
ASKER
Thanks.
So how do I use file with my criteria and a given directory? Can you please give me the full syntax.
variables: $customer, $yesterday, $dir
expected back is @files
So how do I use file with my criteria and a given directory? Can you please give me the full syntax.
variables: $customer, $yesterday, $dir
expected back is @files
what filenames would you want to match?
ASKER
File name should contain the customer name ($customer) and yesterday's date ($yesterday). Recursive within $dir folder.
use File::Find;
our @array;
our $custre=qr/\Q$customer\E/;
our $datere=qr/\Q$yesterday\E/ ;
find(sub{/$custre/&&/$date re/&&push @array,$File::Find::name}, $dir);
our @array;
our $custre=qr/\Q$customer\E/;
our $datere=qr/\Q$yesterday\E/
find(sub{/$custre/&&/$date
ASKER
That works!! One more addition to it.
It works if the file name has the customer name. But, if the file is in a folder that has the customer name, it doesnt work.
For example:
/opt/files/AAA/file_201206 29 -- Doesn't work
/opt/files/AAA/AAA_file_20 120629 -- Works
/opt/files/AAA_file_201206 29 -- Works.
Can you get it to work for the above scenario also please.
Bumping up the points to 200.
It works if the file name has the customer name. But, if the file is in a folder that has the customer name, it doesnt work.
For example:
/opt/files/AAA/file_201206
/opt/files/AAA/AAA_file_20
/opt/files/AAA_file_201206
Can you get it to work for the above scenario also please.
Bumping up the points to 200.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
THanks. That works perfectly.
Can you also look at my other qn regarding scp command.
Can you also look at my other qn regarding scp command.
ASKER
Hi Ozo,
How can I do the exact search on a remote server?
Can you please answer the other qn I posted.
https://www.experts-exchange.com/questions/27793310/Find-files-on-remote-machine-and-copy-using-SCP.html
How can I do the exact search on a remote server?
Can you please answer the other qn I posted.
https://www.experts-exchange.com/questions/27793310/Find-files-on-remote-machine-and-copy-using-SCP.html
opendir DIR,"path/to/directory" or die "can't open dir";
@files = grep /$yesterday/, readdir(DIR);
will work to find files in the open directory which contain $yesterday
An easier way to find the same files might be
chdir "path/to/directory";
@files = <*$yesterday*>;