By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

Searching files by matching filename pattern and concatenating contents of the files

P: 37
Hi,
I wanted to write a Perl script that searches a given folder for all files that have filenames based on the previous day's date.

eg. if the filenames of the files in the said folder are ....
server1_Statistics_0.20090618T124050
server1_Statistics_0.20090619T080652
server1_Statistics_0.20090220T124502
server1_Statistics_0.20090621T105927
server1_Statistics_0.20090621T105638
server1_Statistics_0.20090621T105611
server1_Statistics_0.20090621T105518
server1_Statistics_0.20090621T105108

find all files with yesterday's date(20090621) and concatenate their contents into a single file with the filename "server1_Statistics_20090621"
Jun 22 '09 #1
Share this Question
Share on Google+
8 Replies


Expert Mod 100+
P: 589
What have you tried?

What errors and/or warnings are you receiving?

What portion of the task do you not know how to accomplish?
Jun 22 '09 #2

P: 37
I am relatively new to Perl and we have it installed on AIX. I have worked with the same on Linux and when I tried getting the previous day's date itself, I am having trouble...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $today=`date --date='1 day ago' +%Y%m%d`;
  4.  
  5.  
I tried using the following command, but I was having trouble parsing the columns, to get only the date ...

Expand|Select|Wrap|Line Numbers
  1. perl -e 'print localtime(time() - 86400) . "\n"'
  2.  
Even after the date has been captured, the month is shown in words, whereas I want it to match the filenames which are numerals.
Jun 22 '09 #3

Expert Mod 100+
P: 589
Use the strftime function from the POSIX module.

Expand|Select|Wrap|Line Numbers
  1. use POSIX 'strftime';
  2.  
  3. my $date = strftime("%Y%m%d", localtime(time - 86400));
  4. print $date;
  5.  
Jun 22 '09 #4

P: 37
That worked fine, thanks.

I am now trying to include the output from that into a search for filenames containing yesterday's date.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $hostname=`hostname`;
  3. chomp($hostname);
  4. #$today=`perl -e 'print localtime(time() - 86400) . "\n"'`;
  5. #chomp($today);
  6.  
  7. #!/usr/bin/perl
  8.  
  9. use POSIX 'strftime';
  10.  
  11. my $date = strftime("%Y%m%d", localtime(time - 86400)). "\n";
  12. #print $date;
  13.  
  14. $wldir="/usr/IBM/WebSphere/AppServer/logs/test";
  15. #$wlarc=$wldir/$date;
  16.  
  17. #print "Archiving $wldir directory\n";
  18. do_dir($wldir);
  19.  
  20.  
  21.  
  22. sub do_dir {
  23.     my $dir = shift;
  24.     opendir(D, $dir);
  25.     my @f = readdir(D);
  26.     closedir(D);
  27.     foreach my $file (@f)
  28.        {
  29.         my $filename = $dir . '/' . $file;
  30.         if ($file eq '.' || $file eq '..')
  31.           {
  32.           }
  33.         elsif (-d $filename)
  34.           {
  35.             # depending on your needs you can do subdirs
  36.             do_dir($filename);
  37.           }
  38.         else {
  39.             # do something with $filename, like ...
  40.             if ($filename =~ m/$date/)
  41.               {
  42.               print "Concatenating $filename\n";
  43.               `cat $filename >> $date`;
  44.               }
  45.              }
  46.       }
  47.   }
  48.  
  49. exit(0);
  50.  
  51.  
  52.  
Even though it doesn't throw an error, it doesn't concatenate either.
Jun 22 '09 #5

P: 37
I tried the following script as well, to no avail...


Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3.  
  4. use POSIX 'strftime';
  5.  
  6. my $date = strftime("%Y%m%d", localtime(time - 86400)). "\n";
  7. #print $date;
  8.  
  9. $wldir="/usr/IBM/WebSphere/AppServer/logs/test";
  10. #$wlarc=$wldir/$date;
  11.  
  12. do_dir($wldir);
  13.  
  14.  
  15.  
  16. sub do_dir {
  17.     my $dir = shift;
  18.     opendir(D, $dir);
  19.     my @f = readdir(D);
  20.     closedir(D);
  21.     foreach my $file (@f)
  22.        {
  23.         my $filename = $dir . '/' . $file;
  24.         if ($file eq '.' || $file eq '..')
  25.           {
  26.           }
  27.         elsif (-d $filename)
  28.           {
  29.             # depending on your needs you can do subdirs
  30.             do_dir($filename);
  31.           }
  32.         else {
  33.             # do something with $filename, like ...
  34.             if ($filename =~ m/server1(.*)\.$date(.*)$/)
  35.               {
  36.               print "Concatenating $filename\n";
  37.               `cat $filename >> $date`;
  38.               }
  39.              }
  40.       }
  41.   }
  42.  
  43.  
If I use ...

Expand|Select|Wrap|Line Numbers
  1. if ($filename =~ m/server1(.*)$/)
  2.  
it works but that takes the contents of all the files in the DIR.
Jun 22 '09 #6

Expert Mod 100+
P: 589
Why are you appending "\n" to the date? That is the main problem, but there are others, such as "useless use of cat".

perldoc -f open
perldoc -f glob
Jun 22 '09 #7

P: 37
Thanks, I got it working once I removed the "\n". I was actually testing it out and forgot to remove it earlier. Thanks, again.
Jun 22 '09 #8

Expert Mod 100+
P: 589
You should look at using:

File::Find - Traverse a directory tree.
http://search.cpan.org/~nwclark/perl...b/File/Find.pm

or

File::Find::Rule - Alternative interface to File::Find
http://search.cpan.org/~rclamp/File-...e/Find/Rule.pm
Jun 22 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.