Replace text in a file

I wrote a program using Perl to find and replace a text within a file.

The text that needs to be replaced in the file is 'sql7.0.1' with 'sqls715'.


When I execute my program I get an error message:

can't /tmp/odbc.ini_old : A file or directory in the path name does not exist. I check the path and the path is correct. My OS is AIX.


Here is my code:


 
#!/usr/bin/perl

use strict;
use warnings;

my $filename = '/tmp/odbc.ini.2014_old';
my $find=' sql7.0.1 ';
my $replace = ' sqls715  ';

{
   local @ARGV =($filename);
   local $^I ='.bac';
   while (<>) {
     if (s/$find/$replace/ig) {
}
 else {
   print;
   }
 }
}
print "Finished"

Open in new window

dfn48Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
Is there any difference if you run this script?
#!/usr/bin/perl
use strict;
use warnings;

my $filename = '/tmp/odbc.ini.2014_old';
my $find=' sql7.0.1 ';
my $replace = ' sqls715  ';

open IN, $filename or die "could not open $filename: $!";
while (<IN>) {
    if (s/$find/$replace/ig) {
    }
    else {
        print;
    }
}
close IN;
print "Finished"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dfn48Author Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.