#!/usr/bin/perl -w use strict; use warnings; my $file_name = "test.txt"; my @ip; my $ip; @ip = (qw/test test1/); open(my $in, '<', $file_name) || die "cannot open $file_name for read $!"; open(my $out, '>', "$file_name.tmp") || die "cannot open $file_name.tmp for write $!"; while( <$in>) { foreach $ip(@ip) { print $out "#" if(/\b$ip\b/i && !/^#/); #only one # at front of line print $out $_; } } close ($in); close ($out); # save copy of original file rename ($file_name,"$file_name.bak") || die "problem with rename $!"; # replace original with the modified version rename ("$file_name.tmp", $file_name) || die "problem with rename $!"; =for I/O files #= file test.txt before running program testing is requrired see/for/test test/data/istesting testing/data/test1 data/data/test2 #= file test.txt after running program testing is requrired testing is requrired #see/for/test see/for/test #test/data/istesting test/data/istesting #testing/data/test1 testing/data/test1 data/data/test2 data/data/test2 =cut