From the Windows command prompt I generate a text file of all the files in a directory:
dir c:\logfiles /B > config.txt
Output:
0001_832ec657.log 0002_a7c8eafc.log
I need to feed the ‘config.txt’ file to another executable, but before I do so, I need to modify the file to add some additional information that the executable needs. So I use the following command:
perl -p -i.bak -e 's/log/log,XYZ/g' config.txt
I’m expecting the result to be:
0001_832ec657.log,XYZ 0002_a7c8eafc.log,XYZ
However, the ‘config.txt’ file is not modified. Using the ‘-w’ option, I get the warning message:
Useless use of a constant in void context at -e line 1.
What am I doing wrong?
Windows
cmd.exedoes not use'as string delimiters, only'. What you’re doing is equivalent to:so
-wis complaining ‘you gave me a string but it does nothing’.The solution is to use double quotes instead:
or to simply leave them off, since there’s no metacharacters in this command that would be interpreted by
cmd.exe.Addendum
cmd.exeis just a really troublesome beast, for anybody accustomed tosh-like shells. Here’s a few other common failures and workarounds regardingperlinvocation.