14

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.log0002_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,XYZ0002_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?

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
askedMar 19, 2009 at 0:30
gatorfax's user avatar
1
  • 1
    This could be the canonical question for how to run Perl one-liners on Windows.CommentedJul 12, 2019 at 19:05

2 Answers2

36

Windowscmd.exe does not use' as string delimiters, only". What you're doing is equivalent to:

perl -p -i.bak -e "'s/log/log,XYZ/g'" config.txt

so-w is complaining "you gave me a string but it does nothing".

The solution is to use double quotes instead:

perl -p -i.bak -e "s/log/log,XYZ/g" config.txt

or to simply leave them off, since there's no metacharacters in this command that would be interpreted bycmd.exe.

Addendum

cmd.exe is just a really troublesome beast, for anybody accustomed tosh-like shells. Here's a few other common failures and workarounds regardingperl invocation.

@REM doesn't work:perl -e"print"@REM works:perl -e "print"@REM doesn't work:perl -e "print \"Hello, world!\n\""@REM works:perl -e "print qq(Hello, world!\n)"
answeredMar 19, 2009 at 1:04
ephemient's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

@ephemient Thanks for this addendum. Very usefull. In my caseperl -e "print \"Hello, world!\n\"" seems to work.
6

ephemient's answer summarizes the problem well, but you do have another option: change your shell fromcmd.exe to a better shell. If you are a Unix type person then I would suggest looking intoCygwin which provides the sort of environment you are used to (for example,Bash and GNU utilities).

If you are a Windows-type person I would suggest looking atPowerShell (née MSH née Monad). In fact, I would suggest looking into PowerShell even if you are Unix type person. It integrates with.NET and has many neat features (like objects being passed through pipes rather than simple lines of text). If I were stuck on a Microsoft OS, it is the shell I would be using.

Other shells for Windows that people seem to like:

answeredMar 20, 2009 at 0:25
Chas. Owens's user avatar

3 Comments

Yeah, or just use double quotes. What if this code has to run on other machines? There is nothing he which would require replacing cmd.
One liners are not meant to be portable code. And how likely is it that Perl will be installed on another Windows machine? Also, if you are a Windows person it is probably a good idea to get used to PowerShell since it is part of 2008 Server.
Cmd.exe sucks badly, if it gets in your way enough a shell change is worth considering. Another shell is worth considering. MSYS and MinGW provide a lighter weight way to get your unix environment than Cygwin. They, too, are worth looking into.mingw.org

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.