http://qs321.pair.com?node_id=873642


in reply to find & replace a string in perl

Two simple solutions come to mind. First, the one-liner:

perl -pi.bak -e "$_ = '# ' . $_ if /\btest\b/" filename.txt

Or the Tie::File approach for simple line by line file handling.

use strict; use warnings; use Tie::File; tie my @filelines, 'Tie::File' or die $!; foreach ( @filelines ) { $_ = '# ' . $_ if /\btest\b/; } untie @filelines or die $!;

I know Tie::File isn't sexy in Perl culture, but it gets the job done without much thought.


Dave