为什么我的 Perl one-liner 在 Windows 上不起作用? [英] Why doesn't my Perl one-liner work on Windows?

查看:32
本文介绍了为什么我的 Perl one-liner 在 Windows 上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Windows 命令提示符生成一个目录中所有文件的文本文件:

dir c:logfiles/B >配置文件

输出:

0001_832ec657.log0002_a7c8eafc.log

我需要将config.txt"文件提供给另一个可执行文件,但在此之前,我需要修改该文件以添加一些可执行文件需要的附加信息.所以我使用以下命令:

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

我期待的结果是:

0001_832ec657.log,XYZ0002_a7c8eafc.log,XYZ

然而,config.txt"文件没有被修改.使用-w"选项,我收到警告消息:

<块引用>

在 -e 第 1 行的 void 上下文中无用地使用常量.

我做错了什么?

解决方案

Windows cmd.exe 不使用 ' 作为字符串分隔符,只有 ".你所做的相当于:

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

所以 -w 抱怨你给了我一个字符串,但它什么也没做".

解决方案是使用双引号:

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

或者干脆不理会它们,因为在这个命令中没有元字符会被 cmd.exe 解释.

附录

cmd.exe 对任何习惯于 sh 之类的 shell 的人来说都是一个非常麻烦的野兽.以下是与 perl 调用有关的其他一些常见故障和解决方法.

<前>@REM 不起作用:perl -e"打印"@REM 工作:perl -e "打印"@REM 不起作用:perl -e "print "Hello, world! ""@REM 工作:perl -e "打印 qq(Hello, world! )"

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.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!
)"

这篇关于为什么我的 Perl one-liner 在 Windows 上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆