On Thu, Oct 14, 2010 at 4:54 PM, yo RO <lyn...@gmail.com> wrote:

> Hello I need to split a log file per days
> I have a file in txt format and I want to create a file with all data
> from one day in one file
> I will give example
>
> I have this imput
> 3_21_2010;11:12\\trafic info
> 3_21_2010;11:34\\trafic info
> 3_21_2010;13:21\\trafic info
> 3_22_2010;11:12\\trafic info
> 3_22_2010;11:34\\trafic info
> 3_22_2010;13:21\\trafic info
> 3_22_2010;11:12\\trafic info
> 3_23_2010;11:34\\trafic info
> 3_23_2010;13:21\\trafic info
> 3_23_2010;13:21\\trafic info
> 3_24_2010;11:12\\trafic info
> 3_24_2010;11:34\\trafic info
> 3_24_2010;13:21\\trafic info
>
> I want to have in output 3 file
> file 1: name  3_21_2010.log
> contain
> 3_21_2010;11:12\\trafic info
> 3_21_2010;11:34\\trafic info
> 3_21_2010;13:21\\trafic info
>
> file 2 : 3_22_2010.log
> contain
>
> 3_22_2010;11:12\\trafic info
> 3_22_2010;11:34\\trafic info
> 3_22_2010;13:21\\trafic info
> 3_22_2010;11:12\\trafic info
>
> and like this
> please help help me with this
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>
This is the trick I would use.

   1. Read the directory containing the files and make an array of all files
   matching your file naming convention (use a regex for the check of the
   filename convention)
   2. Now loop over the array in step one and make a hash using the
   month_day notation as the key and an array of filenames matching this
   month_day notation as the value
   3. For each key in the array create a new file and open all files in the
   value array of that key and write their contents to the new file just
   created. (of course you will have to sort your array first to make sure that
   the logs are opened in the right order)
   4. Optionally remove the old logs by simply unlinking them

You could of course combine step 1 and 2 into a single pass the same goes
for step 3 and 4 but breaking things down in small steps first and then once
you get your head around it working out how to do this more efficient is
usually easier to do.

This sounds to much like a home work assignment to write it all out for you,
if it is I am sure that most of this you have already dealt with in class it
is just the combination of all the different elements that is the challenge.
If it is not then I am sure that by looking up the basic steps one at a time
(Read directory, regular expression, create hash, create/read file and
unlink are the terms you want to search for) you will be able to figure it
out.

Regards,

Rob

Reply via email to