Link to home
Start Free TrialLog in
Avatar of smovva1
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,$mon,$year,$wday,$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";
Avatar of ozo
ozo
Flag of United States of America image

What us the criteria?

  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*>;
Avatar of smovva1
smovva1

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.
without chdir
@files = <path/to/directory/*$yesterday*>;

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.
Avatar of smovva1

ASKER

I like @files = <path/to/directory/*$yesterday*>; 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.
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;
Avatar of smovva1

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
what filenames would you want to match?
Avatar of smovva1

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/&&/$datere/&&push @array,$File::Find::name}, $dir);
Avatar of smovva1

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_20120629  -- Doesn't work
/opt/files/AAA/AAA_file_20120629 -- Works
/opt/files/AAA_file_20120629 -- Works.

Can you get it to work for the above scenario also please.

Bumping up the points to 200.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smovva1

ASKER

THanks. That works perfectly.

Can you also look at my other qn regarding scp command.
Avatar of smovva1

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