| Home Profile Fun |
#135 Linux 03.06.2007
Merging text files with a simple Perl scriptIt is often necessary to operate on text files, to merge and manipulate them. In principle it's always the same. For example, put every line of file1 and file2 together, add another string at the end of each line and write it to file3. The following Perl script can be used as a framework for such operations. It opens two files, merges each line and does some more string operations on it. Finally it writes everything to a third file. It assumes that both files have the same number of lines. Execute it like this: ./mergefiles file1 file2 file3. File3 will be overwritten without warning. #!/usr/bin/perl -w
use strict;
my $i=0;
my $outstring='';
# Get filenames from command line
if(!$ARGV[0] || !$ARGV[1] || !$ARGV[2]) {
print "I need 3 filenames, usage: mergefiles.pl file1 file2 file3\n";
exit 1;
}
my $filename1=$ARGV[0];
my $filename2=$ARGV[1];
my $outfilename=$ARGV[2];
# Arrays to store file content
my @file1=();
my @file2=();
# Open files
open(FILE1, "<$filename1") or die "Cannot open file1 $filename1: $!\n";
open(FILE2, "<$filename2") or die "Cannot open file1 $filename2: $!\n";
open(OUTFILE, ">$outfilename") or die "Cannot open file1 $outfilename: $!\n";
# Read and store file content
while (<FILE1>) { push(@file1,$_); }
while (<FILE2>) { push(@file2,$_); }
# Get number of lines from file1 and file2
# Exit if they are not equal
my $nr1 = scalar @file1;
my $nr2 = scalar @file2;
if ($nr1!=$nr2) {
print "Number of lines of $filename1 and $filename2 must be equal.\n";
exit 1;
}
# Main loop
while ($i<$nr1){
# Remove trailing newlines of each line
chomp($file1[$i]);
chomp($file2[$i]);
#***************************************************************
# Change or add operations here
#
# Create the string that should be written to the outfile
$outstring="Place this string in front, $file1[$i], $file2[$i], and add this string to the end of every line\n";
#
# Do some example modifications:
# replace the commas with semicolons if the string length in file1 is bigger than 10
if (length($file1[$i])>10){
$outstring=~s/,/;/g;
}
#
#***************************************************************
# Write string to outfile
print OUTFILE $outstring;
$i++;
}
$i--;
print "Lines processed: $i\n";
close FILE1;
close FILE2;
close OUTFILE;
exit 0;
|