You can't do that. Seeking to a location in a file and then printing to it overwrites the data at that position.
I suggest you use Tie::File
, which lets you access the contents of a file as an array, so appending to the end of a line of the file is done by simply adding a string to one of the elements of the array.
The code would look like this. Note that the line that creates @newdata
is there just for testing. It creates an array that is the same length as the file, with lines like data1
, data2
etc. as you have in your question.
You should test this on a smaller file initially as it will take a while to process the 15GB file, and it also overwrites it, so if you have any bugs you will destroy your data.
use strict;
use warnings;
use Tie::File;
use Fcntl 'O_RDWR';
tie my @file, 'Tie::File', 'myfile', mode => O_RDWR or die $!;
my @newdata = map sprintf('data%d', $_ + 1), 0 .. $#file;
my $i = 0;
for my $line (@file) {
$line .= "\t" . $newdata[$i];
++$i;
}
untie @file;