perl program to Merging Files into a Single File
This Perl script reads all the files with a given extension in the current directory and merges them into a single file. The merged file name can be passed as a command-line argument.
Method 1:Using file handles
#!/usr/bin/perl
my ($ext, $merged_file) = @ARGV;
my @files = glob "*.$ext";
open my $out_fh, '>', $merged_file or die "Couldn't open $merged_file: $!";
foreach my $file (@files) {
open my $in_fh, '<', $file or warn "Couldn't open $file: $!";
while (my $line = <$in_fh>) {
print $out_fh $line;
}
close $in_fh;
}
close $out_fh;
Save the script as merge.pl and run it with perl merge.pl txt merged.txt. This will merge all the files with the extension "txt" into a single file called "merged.txt".
Method 2: Using cat command
This method uses the cat command to concatenate multiple files into a single file.
#!/usr/bin/perl
use strict;
use warnings;
my ($output_file, @input_files) = @ARGV;
system("cat @input_files > $output_file");
Method 3: Using File::Slurp
This method uses the File::Slurp module to read each input file and append its contents to the output file.
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp;
my ($output_file, @input_files) = @ARGV;
my $output_contents = '';
foreach my $input_file (@input_files) {
$output_contents .= read_file($input_file);
}
write_file($output_file, $output_contents);
Method 4: Using IO::All
This method uses the IO::All module to read each input file and append its contents to the output file.
#!/usr/bin/perl
use strict;
use warnings;
use IO::All;
my ($output_file, @input_files) = @ARGV;
my $output_contents = '';
foreach my $input_file (@input_files) {
$output_contents .= io($input_file)->all;
}
io($output_file)->print($output_contents);
Save the script as merge_files_method4.pl and run it with perl merge_files_method4.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.
Method 5: Using File::MergeSort
This method uses the File::MergeSort module to merge sorted files into a single file.
#!/usr/bin/perl
use strict;
use warnings;
use File::MergeSort;
my ($output_file, @input_files) = @ARGV;
my $sorter = File::MergeSort->new();
$sorter->cmp(sub { $_[0] cmp $_[1] });
foreach my $input_file (@input_files) {
$sorter->load($input_file);
}
$sorter->save($output_file);
Save the script as merge_files_method5.pl and run it with perl merge_files_method5.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt, sorted.
Method 6: Using Path::Tiny
This method uses the Path::Tiny module to read each input file and append its contents to the output file.
#!/usr/bin/perl
use strict;
use warnings;
use Path::Tiny;
my ($output_file, @input_files) = @ARGV;
my $output_contents = '';
foreach my $input_file (@input_files) {
$output_contents .= path($input_file)->slurp;
}
path($output_file)->spew($output_contents);
Save the script as merge_files_method6.pl and run it with perl merge_files_method6.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.
Method 7: Using File::Slurper
This method uses the File::Slurper module to read each input file and append its contents to the output file.
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurper qw(read_text write_text);
my ($output_file, @input_files) = @ARGV;
my $output_contents = '';
foreach my $input_file (@input_files) {
$output_contents .= read_text($input_file);
}
write_text($output_file, $output_contents);
Save the script as merge_files_method7.pl and run it with perl merge_files_method7.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.
Method 8: Using File::ReadBackwards
This method uses the File::ReadBackwards module to read each input file backwards and append its contents to the output file.
#!/usr/bin/perl
use strict;
use warnings;
use File::ReadBackwards;
my ($output_file, @input_files) = @ARGV;
open(my $out_fh, '>', $output_file) or die "Can't open $output_file: $!";
foreach my $input_file (@input_files) {
my $in_fh = File::ReadBackwards->new($input_file) or warn "Can't open $input_file: $!";
while (defined(my $line = $in_fh->readline)) {
print $out_fh $line;
}
}
close($out_fh);
Save the script as merge_files_method8.pl and run it with perl merge_files_method8.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.