perlynoob has asked for the wisdom of the Perl Monks concerning the following question:

Hello, it's my first post so please be gentle ;+} I've just installed Activestate Perl for Windows 5.16.3. I have a text file with a list of names (lower case). I want to replace every occurence of the name 'gary' with the name 'harry'. On the command line i am typing : perl -i.bak -p -e 's/gary/harry/g;' file.txt It returns with no errors but the new file is the same as the old file - no changes have been made. To be honest i don't even know what the -p and -e switches are doing, i'm assuming -i is just for renaming the output file ? Also i don't know what the 's' and 'g' do in the replace bit! Any help greatly appreciated, perlynoob.
  • Comment on Text not being replaced in file via Windows CLI ...

Replies are listed 'Best First'.
Re: Text not being replaced in file via Windows CLI ...
by BillKSmith (Monsignor) on Jul 01, 2014 at 11:56 UTC

    Windows requires double quotes rather than single.

    The activestate installation includes extensive documentation. Double click on the file 'C:\Perl\html\index.html'. This will include links to the documents that you hve already been refered to.

    Bill
Re: Text not being replaced in file via Windows CLI ...
by hippo (Archbishop) on Jul 01, 2014 at 11:36 UTC

    Works fine for me:

    $ cat > foo.tmp gary is the best frank is the worst $ perl -i.bak -p -e 's/gary/harry/g;' foo.tmp $ cat foo.tmp harry is the best frank is the worst

    Bear in mind that by default regular expressions are Case-Sensitive.

    Caveat: I'm using neither Activestate nor Windows.

    To find out about the switches like -p and -e see perlrun. For regular expressions, try perlre. To get started in general try perlintro. Welcome to the wonderful world of Perl!

Re: Text not being replaced in file via Windows CLI ...
by Laurent_R (Canon) on Jul 01, 2014 at 17:39 UTC
    BillKSmith is correct. Try:
    perl -i.bak -p -e "s/gary/harry/g;" file.txt
    Otherwise, you should really read the documentation, but these are quick answers to your question. The command line switches say:
    -i
    Inline editing, i.e. the file itself is modified (but, yes, there is some file renaming behind the scene);
    -p
    Assumes an input loop around the script to read all lines of the original file and print back the modified lines.
    -e
    the text coming between the quotes after this switch is to be taken as the Perl script to be executed (the more common usage, without the -e switch, is to put there name of the file containing the script.

    s/source/target/ is the substitution operator. It replaces source with target. In this specific case, the substitution is applied to the string contained in the default $_ variable, which contains successively each line of the input file. The g modifier says that this substitution should be made as many times as possible (i.e. for all occurrences of source in the current string).

    This was just a very quick answer to your questions (too short to be entirely accurate, but at least you should get the idea), there would much more to say, but go for the documentation you have been pointed to by other monks.
      Thanks all!