Friday, October 17, 2008

Find and Replace

Replace the word new with the word old. /g is used to search globally. /i is used to make case insensitive search.

#!/usr/bin/perl -w

while($_=STDIN)
{
chomp($_);

if (s/new/old/gi)
{
print($_ ."\n");
}
}


If we want to replace only the word new we can use following code.

#!/usr/bin/perl -w

while($_=STDIN)
{
chomp($_);

if (s/\bnew\b/old/gi)
{
print($_ ."\n");
}

}

No comments: