in reply to Re^3: In place editing without reading further
in thread In place editing without reading further
I chose a5 as the line to change and want to only change that line, everything should be exactly the same.HEADER a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 END HEADER b1 b2 c1 c2 .. z1 z2
#!/usr/bin/env perl use strict; use warnings; use Data::Dump; open (my $fh, 'message.txt') or die $!; LINE: while (<$fh>) { last LINE if /END HEADER\s\w/s; } my $headerEndingPositionInBytes = tell($fh); print "Found header ending at $headerEndingPositionInBytes\n"; sysseek $fh,0,0; # Rewind to beginning of file my $header; my $bytesRead = sysread $fh, $header, $headerEndingPositionInBytes; print "Read $bytesRead into header variable\n"; my @lines = split '\n', $header; for (0..$#lines) { $lines[$_] =~ s/^a5$/new magic/; } $header = join "\n",@lines; open (my $newFile, '>','message-fixed.txt') or die "$!"; syswrite($newFile, $header); # Write the header my $blockSize = 32 * 1<<10; #32k my $window; while (my $bytesRead = sysread $fh, $window,$blockSize) { syswrite $newFile, $window, $blockSize; } syswrite $newFile, "\n";
|
|---|