3

I'm trying to execute a simple search and replace at the command line.

I have a file called Test1.txt. The contents are "How are you doing today?"

At the command line, I have navigated to the folder in which the text file resides: C:\perl.

Here are the commands I've issued and the results/messages I'm getting:

perl -pi.bak -e 's/doing/feeling/g' Test1.txt

This creates the backup, but no observable change is made to Test1.txt even though it appears the file was modified as far as Windows explorer is concerned.

perl -pi.bak -e "BEGIN{@ARGV=<Test1.txt>} s/doing/feeling/g"

This makes the change and creates the backup, but I get the message:

-i used with no filenames on the command line, reading from STDIN

Is there a way to use -i with filenames on the command line? I found the BEGIN syntax elsewhere as the way to do it in Windows, but is there no other way to do it without getting this message?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Drew Rush
  • 710
  • 5
  • 19

1 Answers1

3

I am not sure why, but using double quote instead of single makes it work.

perl -pi.bak -e "s/doing/feeling/g" Test1.txt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • From what I've read, double quotes means interpret while single quotes means do not. Not sure what the difference is in this context, however. – Drew Rush Dec 20 '13 at 22:04
  • 2
    @DrewRush `cmd.exe` doesn't treat single quotes as string delimiters. There's a good explanation in the accepted answer to the question I linked above. – ThisSuitIsBlackNot Dec 20 '13 at 22:10
  • For the gory details of dealing command-line parameters to cmd.exe and other Windows executables, see *[A Better Way To Understand Quoting and Escaping of Windows Command Line Arguments](http://www.windowsinspired.com/understanding-the-command-line-string-and-arguments-received-by-a-windows-program/)* and the follow-up post *[How a Windows Program Splits Its Command Line Into Individual Arguments](http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line-into-individual-arguments/)*. – Peter Mortensen May 25 '20 at 17:05