my $rec; sysopen my $fh, "test.txt", O_RDWR | O_CREAT or die "$!"; sysread $fh, $rec, -s $fh; my $offs = 0; while( $offs < length $rec ) { my $next_eol_offs = index $rec, "\n", $offs; $next_eol_offs = length( $rec ) - 1 if $next_eol_offs == -1; my $str = substr $rec, $offs, $next_eol_offs - $offs + 1; # work on $str; note that it includes the newline substr( $rec, $offs, $next_eol_offs - $offs + 1 ) = $str; $offs += length $str; } seek $fh, 0, 0; syswrite $fh, $rec; close $fh or die "$!";