search/replace and update the file in perl script

J

jrefactors

I need a perl script to search and replace the keywords in a file, and
update that file with new
keyword.

The usage should be perl modify.pl oldvalue newvalue filename

I wrote the script, I can search and replace, but I guess
the problem is I don't know how to update the file.

#!/usr/bin/perl
$oldValue = $ARGV[0];
$newValue = $ARGV[1];
$fileName = $ARGV[2];

open(FILE, $fileName) or die("Unable to open file");

# read file into an array
@data = <FILE>;

foreach $line (@data)
{ print $line;
$line =~ s/$oldValue/$newValue/
}


Please advise. Thanks!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top