!/usr/bin/perl -w use strict; use warnings; my $cmd; my $cmd1; $cmd = system("perl -pi -e 's/#//' test.txt"); # removing comments working $cmd1 = system("perl -pi -e 's/(?=.*\btest\b)/#/' test.txt"); #Adding #'s is not working print "$cmd \n"; print "$cmd1\n";