Last active
December 10, 2015 01:48
-
-
Save sureshsaggar/4362796 to your computer and use it in GitHub Desktop.
Perl script to categorize log files in separate output directories depending on the file suffix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use File::Copy; | |
my $usage = <<'END'; | |
# Usage: perl $0 <input_logs_dir> <output_logs_dir> <suffix_regex?> | |
# Example: perl bucket_mqtt_logs.pl ./snapshots/ ./snapshots/ 'log\.((\d{4})-(\d{2})-(\d{2}))-(\d{2})' | |
END | |
if(@ARGV < 2){ | |
print "Error: Insufficient arguments. See usage:\n$usage\n"; exit(0); | |
} | |
my ($input_logs_dir, $output_logs_dir, $suffix) = @ARGV; | |
if(!$suffix){ | |
$suffix = 'log\.((\d{4})-(\d{2})-(\d{2})-(\d{2}))'; | |
} | |
opendir(DIR, $input_logs_dir) or die "Check input_logs_dir [$input_logs_dir]\n". $!; | |
my @dots = readdir(DIR); | |
closedir(DIR); | |
# Scan files under $input_logs_dir | |
# Insert each file into a corresponding bucket inside $output_logs_dir | |
foreach (@dots) { | |
if(-f "$input_logs_dir/$_" && $_ =~ m/$suffix/){ | |
unless(-d "$output_logs_dir/$1"){ | |
mkdir "$output_logs_dir/$1" or die "Unable to create [$output_logs_dir/$1]\n"; | |
} | |
copy("$input_logs_dir/$_","$output_logs_dir/$1/$_") or die "Copy failed for file[$_]: $!"; | |
print "Copied to [$output_logs_dir/$1/$_]\n"; | |
} | |
} | |
print "-" x 80 ."\n"; | |
print "# Input DIR[$input_logs_dir] Suffix[$suffix] Output DIR[$output_logs_dir]\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment