I have a huge text file, and the first five lines of it are read below:
This is fist line
This is second line
This is third line
This is fourth line
This is fifth line
Now I want to write something in a random position on the third line of this file, which will replace the characters in this line with the new line I'm writing. I can achieve this with the code below:
use strict;
use warnings;
my @pos = (0);
open my $fh, "+<", "text.txt";
while(<$fh) {
push @pos, tell($fh);
}
seek $fh , $pos[2]+1, 0;
print $fh "HELLO";
close($fh);
However, I cannot understand using the same approach as I can remove the entire third line from this file so that the texts are read below:
This is fist line
This is second line
This is fourth line
This is fifth line
I do not want to read the entire file into an array, and I do not want to use Tie :: File. Is it possible to achieve my requirement using search and storytelling? The solution will be very helpful.
source
share