On Thursday 14 October 2010 16:54:32 yo RO 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
Try this program: [code] #!/usr/bin/perl use strict; use warnings; # Replace this pseudo-constant with the name of the test log to open. my $LOG_FILE = "test.log"; open my $log_in_fh, "<", $LOG_FILE or die "Cannot open '$LOG_FILE' for reading - $!"; my $log_out_fh; my $log_out_date; while (my $line = <$log_in_fh>) { chomp($line); if (my ($date) = $line =~ m{\A(\d+_\d+_\d+);}) { if (!defined($log_out_date) or $log_out_date ne $date) { if ($log_out_fh) { close($log_out_fh); } $log_out_date = $date; open $log_out_fh, ">", "$log_out_date.log" or die "Cannot open '$log_out_date.log' for writing - $!"; } print {$log_out_fh} "$line\n"; } else { # Malformatted line - what now? } } close($log_out_fh); [/code] You may need to customise it a little. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ My Aphorisms - http://www.shlomifish.org/humour.html <rindolf> She's a hot chick. But she smokes. <go|dfish> She can smoke as long as she's smokin'. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/