From what I understand you want to replace a bit of text in a given row and columns with some other text. This would be like this:
use strict; use warnings; my ($file_nm,$line_nb,$col_start,$col_end,$string)=@ARGV; open FILE, "<", $file_nm or die "Cannot open file $file_nm!\n"; open OUT, ">", "~$file_nm" or die "Cannot open temp file ~$file_nm for + writing!\n"; while(<FILE>){ substr( $_, $col_start, $col_end-$col_start+1 ) = $string if $.==$ +line_nb; print OUT $_; } close OUT; close FILE; rename( "~$file_nm", $file_nm );
Another comment: Your last line substituting the whole line with the new one would replace all lines of the same content. So if the target line happens to appear multiple times it would be replaced multiple times...
UPDATE: Writing file in place added.
In reply to Re: Replace specific lines specific range of characters.
by hdb
in thread Replace specific lines specific range of characters.
by govindkailas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |