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

后端 未结 2 1086
执笔经年
执笔经年 2020-11-30 11:28

From the Windows command prompt I generate a text file of all the files in a directory:

dir c:\\logfiles /B > config.txt

Output:

2条回答
  •  旧时难觅i
    2020-11-30 11:53

    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!\n\""
    @REM works:
    perl -e "print qq(Hello, world!\n)"
    

提交回复
热议问题