#!/usr/bin/perl -w use strict; my $numArgs = $#ARGV + 1; # calculate number of arguments passed to program from command line my $inputfile; my $inputfile2; my $outputfile; # initialise filename variables my $reqdcolumn; if ($numArgs < 1) { die "Please choose a column to append"; } elsif ($numArgs >= 4) { $inputfile = "$ARGV[1]"; print "Input filename1 received from command line. Opening '$inputfile'...\n"; $inputfile2 = "$ARGV[2]"; print "Input filename2 received from command line. Opening '$inputfile2'...\n"; $outputfile = "$ARGV[3]"; print "Output filename received from command line. Opening '$outputfile'...\n"; $reqdcolumn = $ARGV[0]; print "Required column received from command line. Appending column $reqdcolumn of '$inputfile' to '$inputfile2'...\n"; } elsif ($numArgs == 3) { $inputfile = "$ARGV[1]"; print "Input filename1 received from command line. Opening '$inputfile'...\n"; $inputfile2 = "$ARGV[2]"; print "Input filename2 received from command line. Opening '$inputfile2'...\n"; $reqdcolumn = $ARGV[0]; print "Required column received from command line. Appending column $reqdcolumn of '$inputfile' to '$inputfile2'...\n"; print "Please enter the path of your output file: "; chomp($outputfile = ); } elsif ($numArgs == 2) { $inputfile = "$ARGV[1]"; print "Input filename1 received from command line. Opening '$inputfile'...\n"; print "Please enter the path of your second input file: "; chomp($inputfile = ); $reqdcolumn = $ARGV[0]; print "Required column received from command line. Appending column $reqdcolumn of '$inputfile' to '$inputfile2'...\n"; print "Please enter the path of your output file: "; chomp($outputfile = ); } elsif ($numArgs == 1) { print "Please enter the path of your first input file: "; chomp($inputfile = ); print "Please enter the path of your second input file: "; chomp($inputfile = ); $reqdcolumn = $ARGV[0]; print "Required column received from command line. Appending column $reqdcolumn of '$inputfile' to '$inputfile2'...\n"; print "Please enter the path of your output file: "; chomp($outputfile = ); } else { print "Please enter the path of your first input file: "; chomp($inputfile = ); print "Please enter the path of your second input file: "; chomp($inputfile = ); print "Please choose the number of the column in inputfile1 to append to inputfile2 (first column is zero): "; chomp($reqdcolumn = ); print "Please enter the path of your output file: "; chomp($outputfile = ); } open INPUTFILE, "<$inputfile" || die "An error occured whilst opening your input file '$inputfile': $!"; open OUTPUTFILE, ">$outputfile" || die "An error occured whilst creating your output file '$outputfile': $!"; print "INPUT: $inputfile\n"; print "OUTPUT: $outputfile\n"; #display input and output file names my @fields; my $fields; my @selectedcolumn; my $selectedcolumn; my $lineindex = 0; while(defined(my $line = )) { # read through each line in turn while they still exist $line =~ s/[\n]+//g; # remove new line characters @fields = split(/,\t*/, $line); # split line into fields according to comma delimiter $selectedcolumn[$lineindex] = $fields[$reqdcolumn]; $lineindex++; } $lineindex = 0; close INPUTFILE; open INPUTFILE, "<$inputfile2" || die "An error occured whilst opening your input file '$inputfile': $!"; while(defined(my $line = )) { # read through each line in turn while they still exist $line =~ s/[\n]+//g; # remove new line characters my $newcolumn = $selectedcolumn[$lineindex]; print OUTPUTFILE "$line,$newcolumn\n"; $lineindex++; } close INPUTFILE; close OUTPUTFILE; # close input and output files