#!/usr/bin/perl use strict; use warnings; { local ( $^I, @ARGV ) = ( '', 'test.txt' ); while ( <> ) { s/#//; print; } } { local ( $^I, @ARGV ) = ( '', 'test.txt' ); while ( <> ) { s/(?=.*\btest\b)/#/; print; } }