Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Dallas
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Perl Simple Search/Replace is not working

    Hello, I am attempting to perform a simple search replace into a text file, but nothing seems to be replaced. My file is not read only. I can pull the contents out of this text file into a log file, which means I can access it. Am I missing something?

    Any help would be appreciated, even though I know this is basic. Thanks.

    #!/usr/bin/perl
    use File::Find;
    open (logfilefsv, '>>logfsv.txt');
    $file = "C:/TestPerlFSV/Mark.txt";
    open(IN, "+<$file");
    @lines=<IN>;
    print logfilefsv @lines;
    foreach $file (@lines) {s/N/M/gi;}

    I am trying to get beyond this so I can automate a test environment and part of it is modifying configuration files.
    Last edited by msstein; 06-24-2012 at 07:45 PM.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Move your print statement after the foreach loop.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Dallas
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I moved the print statement after my foreach loop, but my text file was not updated. The log file is updated, but the actual text file is not updated.

    #!/usr/bin/perl
    use File::Find;
    open (logfilefsv, '>>logfsv.txt');
    $file = "C:/TestPerlFSV/Mark.txt";
    open(IN, "+<$file");
    @lines=<IN>;
    foreach $file (@lines) {s/N/M/gi;}
    print logfilefsv @lines;
    Last edited by msstein; 06-24-2012 at 11:09 PM.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    When you say "the actual text file is not updated", I assume you mean the "Mark.txt' file. Given your posted code, why would you expect Mark.txt to be updated?

  • #5
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Dallas
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi. Well, I thought since $file is pointing to Mark.txt that the search/replace expression would work for the contents in this file.

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Your "search/replace" was applied to an array which is in memory. The code you posted never outputs that array to the Mark.txt file.

    Actually, to be more precise, your substitution was applied to the scalar $file and was thrown away at each iteration of the loop. The @lines array was never modified.
    Last edited by FishMonger; 06-26-2012 at 02:56 AM.

  • #7
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Dallas
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like the following will do what I am needing: Thanks. Mark

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $filename = "C:/Perl/Mark.txt";
    my $find = 'N';
    my $replace = 'M';

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

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    That reduces to this 1 liner.

    perl -pi.bak -e "s/N/M/ig" C:/Perl/Mark.txt


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •