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

copy few lines from one file to another file.

P: 2
Hi friends
I am new to perl so please guide me.

I have one application which created backup log file every day.But it appends that file so you can see logs for different day in one file only.

My requirement is to copy backup log for the specific day (yesterday) and write in other file.
That file will be mailed to admin for ready reference.

So here is some text from that log file

Expand|Select|Wrap|Line Numbers
  1. ======== Mon Oct 22 23:00:00 2007: BEGIN NNM Backup.
  2.      * Options:
  3.          Staging area =  F:\NNM_ovbackup/ovbackup
  4.          Operational =  0
  5.          Analytical =  0
  6.          ovresume timeout = default
  7.      * Starting operational checkpoint step. (Mon Oct 22 23:01:49 2007)
  8.        Running pre_pause phase.
  9.        Running ovpause.
  10.        ovpause succeeded.
  11.          Running script: nnm_checkpoint.ovpl.
  12. ovresume timeout =:       default 
  13.              Copy data in E:/HP OpenView/NNM/databases/openview
  14.              Copy data in E:/HP OpenView/NNM/databases/eventdb
  15.              Copy data in E:/HP OpenView/NNM/log
  16.              Copy data in E:/HP OpenView/NNM/conf
  17.              Copy data in E:/HP OpenView/NNM/registration
  18.              Copy data in E:/HP OpenView/NNM/fields
  19.              Copy data in E:/HP OpenView/NNM/symbols
  20.              Copy data in E:/HP OpenView/NNM/lrf
  21.              Copy data in E:/HP OpenView/NNM/www/htdocs
  22.              Copy data in E:/HP OpenView/NNM/www/images
  23.              Copy data in E:/HP OpenView/NNM/www/protected
  24.              Copy data in E:/HP OpenView/NNM/databases/nnmet
  25.              Copy SNMP Configuration Database. 
  26.        Running ovresume.
  27.        ovresume succeeded.
  28.        Running post_resume phase.
  29.      * Starting analytical checkpoint step. (Mon Oct 22 23:31:54 2007)
  30.          Running script: nnm_checkpoint.ovpl.
  31. ovresume timeout =:       default 
  32.           Starting backup of SNMP Collect data. 
  33.              Running ovpause -v snmpCollect 
  34.              ovpause succeeded. 
  35.                 Running ovresume.
  36.              ovresume succeeded. 
  37.           Backup of SNMP Collect data completed successfully. 
  38.           Starting backup of Data Warehouse data. 
  39.           Backup of NNM Data Warehouse completed successfully. 
  40.  
  41. ======== Mon Oct 22 23:32:18 2007: END NNM Backup.
  42.  
  43. ======== Tue Oct 23 23:00:00 2007: BEGIN NNM Backup.
  44.      * Options:
  45.          Staging area =  F:\NNM_ovbackup/ovbackup
  46.          Operational =  0
  47.          Analytical =  0
  48.          ovresume timeout = default
  49.      * Starting operational checkpoint step. (Tue Oct 23 23:01:10 2007)
  50.        Running pre_pause phase.
  51.        Running ovpause.
  52.        ovpause succeeded.
  53.          Running script: nnm_checkpoint.ovpl.
  54. ovresume timeout =:       default 
  55.              Copy data in E:/HP OpenView/NNM/databases/openview
  56.              Copy data in E:/HP OpenView/NNM/databases/eventdb
  57.              Copy data in E:/HP OpenView/NNM/log
  58.              Copy data in E:/HP OpenView/NNM/conf
  59.              Copy data in E:/HP OpenView/NNM/registration
  60.              Copy data in E:/HP OpenView/NNM/fields
  61.              Copy data in E:/HP OpenView/NNM/symbols
  62.              Copy data in E:/HP OpenView/NNM/lrf
  63.              Copy data in E:/HP OpenView/NNM/www/htdocs
  64.              Copy data in E:/HP OpenView/NNM/www/images
  65.              Copy data in E:/HP OpenView/NNM/www/protected
  66.              Copy data in E:/HP OpenView/NNM/databases/nnmet
  67.              Copy SNMP Configuration Database. 
  68.        Running ovresume.
  69.        ovresume succeeded.
  70.        Running post_resume phase.
  71.      * Starting analytical checkpoint step. (Tue Oct 23 23:30:40 2007)
  72.          Running script: nnm_checkpoint.ovpl.
  73. ovresume timeout =:       default 
  74.           Starting backup of SNMP Collect data. 
  75.              Running ovpause -v snmpCollect 
  76.              ovpause succeeded. 
  77.                 Running ovresume.
  78.              ovresume succeeded. 
  79.           Backup of SNMP Collect data completed successfully. 
  80.           Starting backup of Data Warehouse data. 
  81.           Backup of NNM Data Warehouse completed successfully. 
  82.  
  83. ======== Tue Oct 23 23:31:16 2007: END NNM Backup.
  84.  
so i want to copy from today's date say ' Tue Oct 23 23:00:00 2007: BEGIN NNM Backup." till end of a file."


Thanks in Advance
Mahesh
Oct 24 '07 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
First, you didn't post a question, you just posted a synopsis of what you are doing. Being that this is a learning forum, and not a script writing service, you will need to do a lot of the legwork. Why not try some code to do what you want to do and if you get stuck, please post the code here (in proper code tags) and we will assist you in troubleshooting it.

Regards,

Jeff
Oct 24 '07 #2

P: 2
Hi Friends

Here i have writeen the program and getting the output by using print.

But i want output in a txt file. So how to get output of array to txt file.

here is my program

Expand|Select|Wrap|Line Numbers
  1. use File::Copy;
  2.  
  3. $ovbackupfile = "ovbackup.log.";
  4.  
  5. $my_test_file = "copyovbackup.log.";
  6.  
  7. $my_output_file = "output.txt";
  8.  
  9. copy($ovbackupfile,$my_test_file) or die "file can not be copied.";
  10.  
  11. my $time =localtime;
  12.  
  13. my @splitdate = split(/ /,$time);
  14.  
  15. print "this is array  $splitdate[0] $splitdate[1] $splitdate[2] $splitdate[4] \n";
  16.  
  17. $date="$splitdate[0] $splitdate[1] $splitdate[2] 23:00:00 $splitdate[4]: BEGIN NNM Backup.\n";
  18.  
  19. print "Date =$date";
  20.  
  21. print "this is current time : $time \n";
  22.  
  23. open  myhandle, "G:/test/copyovbackup.log" or die "unable to open : $! \n";
  24.     while (<myhandle>) 
  25.             {
  26.  
  27.                 if ($_ =~ m/$date/) 
  28.                     {
  29.                     print 'match';
  30.                     print "read this is :$_\n";
  31.                     #@lines =  <myhandle>;
  32.                     print "@lines";
  33.                     copy(<myhandle>,$my_output_file) or die "output file can not be copied.";
  34.                     } 
  35.  
  36.             }
  37.         close myhandle ;
  38.  
  39.  
Oct 26 '07 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Hi Friends

Here i have writeen the program and getting the output by using print.

But i want output in a txt file. So how to get output of array to txt file.

here is my program

Expand|Select|Wrap|Line Numbers
  1. use File::Copy;
  2.  
  3. $ovbackupfile = "ovbackup.log.";
  4.  
  5. $my_test_file = "copyovbackup.log.";
  6.  
  7. $my_output_file = "output.txt";
  8.  
  9. copy($ovbackupfile,$my_test_file) or die "file can not be copied.";
  10.  
  11. my $time =localtime;
  12.  
  13. my @splitdate = split(/ /,$time);
  14.  
  15. print "this is array  $splitdate[0] $splitdate[1] $splitdate[2] $splitdate[4] \n";
  16.  
  17. $date="$splitdate[0] $splitdate[1] $splitdate[2] 23:00:00 $splitdate[4]: BEGIN NNM Backup.\n";
  18.  
  19. print "Date =$date";
  20.  
  21. print "this is current time : $time \n";
  22.  
  23. open  myhandle, "G:/test/copyovbackup.log" or die "unable to open : $! \n";
  24.     while (<myhandle>) 
  25.             {
  26.  
  27.                 if ($_ =~ m/$date/) 
  28.                     {
  29.                     print 'match';
  30.                     print "read this is :$_\n";
  31.                     #@lines =  <myhandle>;
  32.                     print "@lines";
  33.                     copy(<myhandle>,$my_output_file) or die "output file can not be copied.";
  34.                     } 
  35.  
  36.             }
  37.         close myhandle ;
  38.  
  39.  
First, I had asked before that you use the proper code tags to post your code. Please know that that was a request, not a suggestion, as you entering them saves the moderators from cleaning up behind you.

As for your issue, I see you used the copy function from the File::Copy module. That function takes two options, a file1 and a file2. What you have specified is a filehandle for file1 and I don't think that will work as it is a pointer to the file. You will want to possibly create a variable that references the file with its full path. (I could be wrong, but that is my 7am, just rolled out of bed, first observation).

Regards,

Jeff
Oct 26 '07 #4

Post your reply

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