473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
8 3926
RonB
589 Expert Mod 512MB
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
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
RonB
589 Expert Mod 512MB
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
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
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
RonB
589 Expert Mod 512MB
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
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
RonB
589 Expert Mod 512MB
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

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

Similar topics

0
by: Grahammer | last post by:
Just wondering if I can get the Visual Studio .Net IDE editor to treat my ..INC files (included files) the same way as .ASP files? The .INC files are just chunks of ASP code anyhow. Thanks!
2
by: KevinGPO | last post by:
Just wondering if anyone knows if there are converters to convert from: MS Visual C++ 6.0 or MS Visual Studio 2003 project files into UNIX autogen/configure/make files?
4
by: Gary Morrison | last post by:
I need to create a lot of fairly-short audio files from the concatenation of a lot of even shorter audio files. I'd like to control that from a Perl script. The audio files would presumably be...
0
by: Big D | last post by:
Hey there. I'd like to concatenate multiple wav files together. I've got an almost completely working system for it, actually... I'm using AudioSystem.write and my own subclass of...
7
by: pedagani | last post by:
Dear comp.lang.c++, I'm trying to read a file with very long filename using ifstream. Although, the file exists the file open for read fails. Is there a restriction on the size? I'm using winXP...
3
by: Cara83 | last post by:
Hi everyone, I am only new to this Perl programming world and I am having problems with the script I am writing. The program is supposed to search a number of specified directories for files...
0
by: praveenkhade | last post by:
Hello Wenever m opeining the files from the grid its storing in Temprory internet files. the name of the file wen displaying from grid is sum wat like dis (Index.pdf shown as Index.pdf). ...
4
by: unknown | last post by:
Hi, I'm having files with a name pattern like "log_record-YY-MM-DD" and i would like to create a list of the files available. I'm wondering how i can do this, any ideas? Thank you! Ron --...
2
by: postman | last post by:
I need to remove all 8 or 9-digit numbers from memo fields in a table. They are at varying positions within the fields. I was able to construct a SELECT query to retreive all the records that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.