my $MergedFileName="MergeOutput.txt"; # Read the logs file into an array, with .txt extention sorted with timestamp existing in the filename opendir(DIR, '.') or die "Input file not avialable Error #: .$!"; my @filesRead = sort(grep(/\.txt$/,readdir(DIR))); closedir(DIR); # Open the Output file open(MAINOUTPUT,">MergedFileName") || warn "Can't open file\n"; # Start merging file into main file by reading each file, line by line FILE: foreach (@filesRead) { open(FILE, $_) || ((warn "Can't open file $_\n"),next FILE); while () { print MAINOUTPUT $_; } close(FILE); } close(MAINOUTPUT); # Merging of the files is done