#!/usr/bin/perl use strict; use warnings; #------------------------------------------------------- sub generate_date_str ($) { my ($time) = @_; my ($mday,$mon,$year) = (localtime($time))[3,4,5]; $year += 1900; $mon++; return sprintf("%04d%02d%02d", $year, $mon, $mday); } #------------------------------------------------------- sub get_matching_filenames ($$) { my ($dir, $match_str) = @_; opendir(DIR, $dir) or die "couldn't open directory \"$dir\""; my @names = grep {/$match_str/} readdir(DIR); closedir(DIR); return @names; } #------------------------------------------------------- sub consolidate_logs ($$$) { my ($destination_file, $dir, $filename_str) = @_; my @files = get_matching_filenames($dir, $filename_str); open(OUT,"> $destination_file") or die "Could not open file \"$destination_file\" for writing"; foreach my $source_file (@files) { print "Processing of log \"$source_file\" started at " . localtime() . "\n"; open(OLD,"< $dir/$source_file") or die("Could not open file \"$dir/$source_file\" for reading"); while () { print OUT $_; } close(OLD); print "Processing of log \"$source_file\" ended at " . localtime() . ".\n"; } close(OUT); } #------------------------------------------------------- sub split_logs ($$$) { my ($source_file, $business_list, $filename_prefix) = @_; foreach my $business (@$business_list) { my ($domain, $file) = @$business; my $outfile = "/inside29/urchin/test/newfeed/$filename_prefix-$file"; my $newigebusiness = $domain; print "Creating of log for $newigebusiness started at " . localtime() . "\n"; open(OUT,">> $outfile") || die("Could not open out file \"$outfile\" for appending"); open(OLD,"< $source_file") || die ("Could not open the consolidated file \"$source_file\" for reading"); while () { if ((index($_,$newigebusiness))> -1) { print OUT $_; } } close(OLD); close(OUT); print "Log for $newigebusiness created at " . localtime() . "\n"; } } #------------------------------------------------------- my @businesses = ( [ "\"corp.home.ge.com\"", "new_corp_home_ge_com.log" ], [ "\"scotland.gcf.home.ge.com\", "new_scotland_gcf_home_ge_com.log" ], [ "\"marketing.ge.com\"", "new_marketing_ge_com.log" ] ); my $consolidated_log = "consolidatedlog.txt"; my $logfiles_dir = '/inside29/urchin/test/logfiles'; my $today = generate_date_str( time() ); my $yesterday = generate_date_str( time() - (24 * 60 * 60) ); consolidate_logs($consolidated_log, $logfiles_dir, $yesterday); split_logs($consolidated_log, \@businesses, $today);