Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

Why doesn't my Perl one-liner work on Windows?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Windows cmd.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 by cmd.exe.

Addendum

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

@REM doesn't work:
perl -e"print"
@REM works:
perl -e "print"
@REM doesn't work:
perl -e "print "Hello, world!
""
@REM works:
perl -e "print qq(Hello, world!
)"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...