0

in perl, I am trying to open a file , find a string, replace the string, and close the file. I have scoured the internet, and this is what I have come up with:

  #!/usr/bin/perl -w
  #  use warnings;  

    my $oldUrl = "someString";
    my $newUrl = "someOtherString";
    $file = "someFile";
    open (MYFILE,"$file") or die "Can't open '$file': $!";
    while(my $row = <MYFILE>){
            if($row =~ /$oldUrl/)
            {
                    $row = "$newUrl";
                    print MYFILE $row;
            }
    }
    close(MYFILE);

this does nothing except print to screen the text: someString. I cant get the newUrl to get actually written in the file, what am i missing?

how would i do all files in a dir that end in .cfg:

  #!/usr/bin/perl -w
use warnings;

my $directory = '/tftpboot';

my $oldUrl = "account.1.sip_server_host = s150133.trixbox.fonality.com";
my $newUrl = "account.1.sip_server_host = 162.221.24.130";
 ....
  .....
1
  • 1
    open(FOO, $file) opens $file for reading, but you're also trying to write to it. That won't work. Also, this won't fix your particular issue, but in general, you should use the safer, three-argument form of open and lexical filehandles: open my $fh, '<', $file or die $!; Commented Aug 18, 2014 at 21:25

2 Answers 2

3

Please read: perlfaq5 - How do I change, delete, or insert a line in a file, or append to the beginning of a file?

I would recommend using $INPLACE_EDIT to handle the opening and closing of the file for you. However, other methods are listed in the above reference:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;

my $file = "someFile";
my $oldUrl = "someString";
my $newUrl = "someOtherString";

local @ARGV = $file;
local $^I = '.bak';
while (<>) {
    s/.*$oldUrl.*/$newUrl/;   # Replacing the whole line seems odd, but it's what you did in your example.
    print;
}
#unlink "$file$^I"; #Optionally delete backup
Sign up to request clarification or add additional context in comments.

3 Comments

Typically sed would have barfed on those delimiters if the variables contained the delimiter characters. Odd that perl didn't complain.
Miller. how would i do this with a whole directory? see edit.
To process an entire directory, you simply need to load it using opendir and readdir, and remember to specify the full path information when attempting to modify the file. Alternatively, you could use a module like Path::Class to work in a more platform independent way.
1

You can use this one-liner to replace string in a file:

perl -pi.bak -e 's/someString/someOtherString/g' someFile

2 Comments

Yea, the only problem is I need to loop through a bunch of files.. So I think it needs to be a script?
instead of someFile, you can also use *.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.