Why is this Perl command failing on the Windows command line?












0















I am trying to replicate the Perl command from this answer on Ask Ubuntu in a Windows command line environment. I believe I have downloaded Perl for Windows and installed it correctly, so the error seems to be one of syntax.



The original command in a Linux shell is:



ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


I believe the Windows version should be:



dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv


However, this is the error I'm getting:



Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg".
'/'' is not recognized as an internal or external command,
operable program or batch file.


What is the correct syntax for running this Perl command in Windows?










share|improve this question




















  • 3





    Try what you have with the linux command but change single quotes to double quotes.

    – barlop
    Feb 12 at 1:25











  • The . you have in the linux one might apply to the windows one too, so keep that. (assuming your linux line is fine)

    – barlop
    Feb 12 at 1:25













  • And basic troubleshooting is try something simple so s/a/b/ rather than s/............/........../ And eventually you might figure out what it is

    – barlop
    Feb 12 at 1:27


















0















I am trying to replicate the Perl command from this answer on Ask Ubuntu in a Windows command line environment. I believe I have downloaded Perl for Windows and installed it correctly, so the error seems to be one of syntax.



The original command in a Linux shell is:



ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


I believe the Windows version should be:



dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv


However, this is the error I'm getting:



Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg".
'/'' is not recognized as an internal or external command,
operable program or batch file.


What is the correct syntax for running this Perl command in Windows?










share|improve this question




















  • 3





    Try what you have with the linux command but change single quotes to double quotes.

    – barlop
    Feb 12 at 1:25











  • The . you have in the linux one might apply to the windows one too, so keep that. (assuming your linux line is fine)

    – barlop
    Feb 12 at 1:25













  • And basic troubleshooting is try something simple so s/a/b/ rather than s/............/........../ And eventually you might figure out what it is

    – barlop
    Feb 12 at 1:27
















0












0








0








I am trying to replicate the Perl command from this answer on Ask Ubuntu in a Windows command line environment. I believe I have downloaded Perl for Windows and installed it correctly, so the error seems to be one of syntax.



The original command in a Linux shell is:



ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


I believe the Windows version should be:



dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv


However, this is the error I'm getting:



Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg".
'/'' is not recognized as an internal or external command,
operable program or batch file.


What is the correct syntax for running this Perl command in Windows?










share|improve this question
















I am trying to replicate the Perl command from this answer on Ask Ubuntu in a Windows command line environment. I believe I have downloaded Perl for Windows and installed it correctly, so the error seems to be one of syntax.



The original command in a Linux shell is:



ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


I believe the Windows version should be:



dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv


However, this is the error I'm getting:



Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg".
'/'' is not recognized as an internal or external command,
operable program or batch file.


What is the correct syntax for running this Perl command in Windows?







windows command-line perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 at 16:14









phuclv

10.5k64295




10.5k64295










asked Feb 12 at 1:09









QuestionerQuestioner

2383728




2383728








  • 3





    Try what you have with the linux command but change single quotes to double quotes.

    – barlop
    Feb 12 at 1:25











  • The . you have in the linux one might apply to the windows one too, so keep that. (assuming your linux line is fine)

    – barlop
    Feb 12 at 1:25













  • And basic troubleshooting is try something simple so s/a/b/ rather than s/............/........../ And eventually you might figure out what it is

    – barlop
    Feb 12 at 1:27
















  • 3





    Try what you have with the linux command but change single quotes to double quotes.

    – barlop
    Feb 12 at 1:25











  • The . you have in the linux one might apply to the windows one too, so keep that. (assuming your linux line is fine)

    – barlop
    Feb 12 at 1:25













  • And basic troubleshooting is try something simple so s/a/b/ rather than s/............/........../ And eventually you might figure out what it is

    – barlop
    Feb 12 at 1:27










3




3





Try what you have with the linux command but change single quotes to double quotes.

– barlop
Feb 12 at 1:25





Try what you have with the linux command but change single quotes to double quotes.

– barlop
Feb 12 at 1:25













The . you have in the linux one might apply to the windows one too, so keep that. (assuming your linux line is fine)

– barlop
Feb 12 at 1:25







The . you have in the linux one might apply to the windows one too, so keep that. (assuming your linux line is fine)

– barlop
Feb 12 at 1:25















And basic troubleshooting is try something simple so s/a/b/ rather than s/............/........../ And eventually you might figure out what it is

– barlop
Feb 12 at 1:27







And basic troubleshooting is try something simple so s/a/b/ rather than s/............/........../ And eventually you might figure out what it is

– barlop
Feb 12 at 1:27












2 Answers
2






active

oldest

votes


















1














Windows uses | as the pipe character too. Copy the command directly, but replace ls with dir and filename with expenses.csv



Added as answer because I don't have the reputation to comment






share|improve this answer
























  • In conjunction with some of the comments, this was the command that actually ended up working: dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/" >> filename.csv

    – Questioner
    Feb 12 at 8:14






  • 1





    your solution won't work because single quotes are not special in cmd

    – phuclv
    Feb 12 at 11:27



















3














The other answer is not correct. If you do that you'll still get errors when running in cmd





C:Users>dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
Can't find string terminator "'" anywhere before EOF at -e line 1.
'/'' is not recognized as an internal or external command,
operable program or batch file.


The reason is due to multiple issues in the command




  • Single quote ' is not a quoting symbol in cmd.exe


  • Comma ,, semicolon ; and equals = are also delimiters beside space and and tab like other shells


Even / sometimes separates command arguments like dir/b/c/d or yourcommand/param but luckily it doesn't apply in this case. So 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' will be passed as multiple parameters if there are any word separator in it, which is , in this case, and the command will be equivalent to



perl  -pe  's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2   $3-$4-$5   $6   $   & /'


and your proposed command dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv will be parsed as



>>filename.csv >perl dir -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2  $3-$4-$5  $6  $ & /'


since >file redirection can appear at any places. That causes the Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg" you saw above, because that's not a valid switch to dir



That's also the reason why there's an error from perl Can't find string terminator "'" anywhere before EOF at -e line 1 apart from the lines from cmd because in the first parameter begins with ' but there's no closing '. It's easy to check how it's split into parameters:



E:>type testparam.bat
@echo off
:loop
if "%1"=="" goto :exit
echo "%1"
shift
goto :loop
:exit

E:>testparam -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
"-pe"
"'s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2"
"$3-$4-$5"
"$6"
"$"
'/'' is not recognized as an internal or external command,
operable program or batch file.


Now look at the output and notice the funny things happen: because of the ampersand, &/' will end the previous command and execute a command named /' which doesn't exist and outputs an error like above





The best solution is to use PowerShell, where you can run the command with just a slight change because single quote is also used for quoting in PowerShell



ls |% { $_.Name } | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


ls along with dir and gci in PowerShell alias to Get-ChildItem. And you need |% (which is an alias of ForEach-Object) to print the object names. An alternative in PowerShell is



cmd /c dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


I've left out the redirection for clarity. The output will be printed to stdout. If you want to redirect it to file just append >filename.csv or >>filename.csv depending on whether you want to overwrite or append



Running the command in cmd would be far more troublesome (and new code should use PowerShell anyway, as it has bene the default options for a long time)



dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2^,$3-$4-$5^,$6^,$^&^/'


In this case changing ' to " also works, but it'll quickly break in many other situations



dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/"




Note that even in Linux your command is also wrong



ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


You need >filename.csv instead of . filename.csv






share|improve this answer


























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1404656%2fwhy-is-this-perl-command-failing-on-the-windows-command-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Windows uses | as the pipe character too. Copy the command directly, but replace ls with dir and filename with expenses.csv



    Added as answer because I don't have the reputation to comment






    share|improve this answer
























    • In conjunction with some of the comments, this was the command that actually ended up working: dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/" >> filename.csv

      – Questioner
      Feb 12 at 8:14






    • 1





      your solution won't work because single quotes are not special in cmd

      – phuclv
      Feb 12 at 11:27
















    1














    Windows uses | as the pipe character too. Copy the command directly, but replace ls with dir and filename with expenses.csv



    Added as answer because I don't have the reputation to comment






    share|improve this answer
























    • In conjunction with some of the comments, this was the command that actually ended up working: dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/" >> filename.csv

      – Questioner
      Feb 12 at 8:14






    • 1





      your solution won't work because single quotes are not special in cmd

      – phuclv
      Feb 12 at 11:27














    1












    1








    1







    Windows uses | as the pipe character too. Copy the command directly, but replace ls with dir and filename with expenses.csv



    Added as answer because I don't have the reputation to comment






    share|improve this answer













    Windows uses | as the pipe character too. Copy the command directly, but replace ls with dir and filename with expenses.csv



    Added as answer because I don't have the reputation to comment







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 12 at 2:06









    BlueDrink9BlueDrink9

    336111




    336111













    • In conjunction with some of the comments, this was the command that actually ended up working: dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/" >> filename.csv

      – Questioner
      Feb 12 at 8:14






    • 1





      your solution won't work because single quotes are not special in cmd

      – phuclv
      Feb 12 at 11:27



















    • In conjunction with some of the comments, this was the command that actually ended up working: dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/" >> filename.csv

      – Questioner
      Feb 12 at 8:14






    • 1





      your solution won't work because single quotes are not special in cmd

      – phuclv
      Feb 12 at 11:27

















    In conjunction with some of the comments, this was the command that actually ended up working: dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/" >> filename.csv

    – Questioner
    Feb 12 at 8:14





    In conjunction with some of the comments, this was the command that actually ended up working: dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/" >> filename.csv

    – Questioner
    Feb 12 at 8:14




    1




    1





    your solution won't work because single quotes are not special in cmd

    – phuclv
    Feb 12 at 11:27





    your solution won't work because single quotes are not special in cmd

    – phuclv
    Feb 12 at 11:27













    3














    The other answer is not correct. If you do that you'll still get errors when running in cmd





    C:Users>dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
    Can't find string terminator "'" anywhere before EOF at -e line 1.
    '/'' is not recognized as an internal or external command,
    operable program or batch file.


    The reason is due to multiple issues in the command




    • Single quote ' is not a quoting symbol in cmd.exe


    • Comma ,, semicolon ; and equals = are also delimiters beside space and and tab like other shells


    Even / sometimes separates command arguments like dir/b/c/d or yourcommand/param but luckily it doesn't apply in this case. So 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' will be passed as multiple parameters if there are any word separator in it, which is , in this case, and the command will be equivalent to



    perl  -pe  's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2   $3-$4-$5   $6   $   & /'


    and your proposed command dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv will be parsed as



    >>filename.csv >perl dir -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2  $3-$4-$5  $6  $ & /'


    since >file redirection can appear at any places. That causes the Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg" you saw above, because that's not a valid switch to dir



    That's also the reason why there's an error from perl Can't find string terminator "'" anywhere before EOF at -e line 1 apart from the lines from cmd because in the first parameter begins with ' but there's no closing '. It's easy to check how it's split into parameters:



    E:>type testparam.bat
    @echo off
    :loop
    if "%1"=="" goto :exit
    echo "%1"
    shift
    goto :loop
    :exit

    E:>testparam -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
    "-pe"
    "'s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2"
    "$3-$4-$5"
    "$6"
    "$"
    '/'' is not recognized as an internal or external command,
    operable program or batch file.


    Now look at the output and notice the funny things happen: because of the ampersand, &/' will end the previous command and execute a command named /' which doesn't exist and outputs an error like above





    The best solution is to use PowerShell, where you can run the command with just a slight change because single quote is also used for quoting in PowerShell



    ls |% { $_.Name } | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


    ls along with dir and gci in PowerShell alias to Get-ChildItem. And you need |% (which is an alias of ForEach-Object) to print the object names. An alternative in PowerShell is



    cmd /c dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


    I've left out the redirection for clarity. The output will be printed to stdout. If you want to redirect it to file just append >filename.csv or >>filename.csv depending on whether you want to overwrite or append



    Running the command in cmd would be far more troublesome (and new code should use PowerShell anyway, as it has bene the default options for a long time)



    dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2^,$3-$4-$5^,$6^,$^&^/'


    In this case changing ' to " also works, but it'll quickly break in many other situations



    dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/"




    Note that even in Linux your command is also wrong



    ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


    You need >filename.csv instead of . filename.csv






    share|improve this answer






























      3














      The other answer is not correct. If you do that you'll still get errors when running in cmd





      C:Users>dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
      Can't find string terminator "'" anywhere before EOF at -e line 1.
      '/'' is not recognized as an internal or external command,
      operable program or batch file.


      The reason is due to multiple issues in the command




      • Single quote ' is not a quoting symbol in cmd.exe


      • Comma ,, semicolon ; and equals = are also delimiters beside space and and tab like other shells


      Even / sometimes separates command arguments like dir/b/c/d or yourcommand/param but luckily it doesn't apply in this case. So 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' will be passed as multiple parameters if there are any word separator in it, which is , in this case, and the command will be equivalent to



      perl  -pe  's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2   $3-$4-$5   $6   $   & /'


      and your proposed command dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv will be parsed as



      >>filename.csv >perl dir -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2  $3-$4-$5  $6  $ & /'


      since >file redirection can appear at any places. That causes the Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg" you saw above, because that's not a valid switch to dir



      That's also the reason why there's an error from perl Can't find string terminator "'" anywhere before EOF at -e line 1 apart from the lines from cmd because in the first parameter begins with ' but there's no closing '. It's easy to check how it's split into parameters:



      E:>type testparam.bat
      @echo off
      :loop
      if "%1"=="" goto :exit
      echo "%1"
      shift
      goto :loop
      :exit

      E:>testparam -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
      "-pe"
      "'s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2"
      "$3-$4-$5"
      "$6"
      "$"
      '/'' is not recognized as an internal or external command,
      operable program or batch file.


      Now look at the output and notice the funny things happen: because of the ampersand, &/' will end the previous command and execute a command named /' which doesn't exist and outputs an error like above





      The best solution is to use PowerShell, where you can run the command with just a slight change because single quote is also used for quoting in PowerShell



      ls |% { $_.Name } | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


      ls along with dir and gci in PowerShell alias to Get-ChildItem. And you need |% (which is an alias of ForEach-Object) to print the object names. An alternative in PowerShell is



      cmd /c dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


      I've left out the redirection for clarity. The output will be printed to stdout. If you want to redirect it to file just append >filename.csv or >>filename.csv depending on whether you want to overwrite or append



      Running the command in cmd would be far more troublesome (and new code should use PowerShell anyway, as it has bene the default options for a long time)



      dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2^,$3-$4-$5^,$6^,$^&^/'


      In this case changing ' to " also works, but it'll quickly break in many other situations



      dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/"




      Note that even in Linux your command is also wrong



      ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


      You need >filename.csv instead of . filename.csv






      share|improve this answer




























        3












        3








        3







        The other answer is not correct. If you do that you'll still get errors when running in cmd





        C:Users>dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
        Can't find string terminator "'" anywhere before EOF at -e line 1.
        '/'' is not recognized as an internal or external command,
        operable program or batch file.


        The reason is due to multiple issues in the command




        • Single quote ' is not a quoting symbol in cmd.exe


        • Comma ,, semicolon ; and equals = are also delimiters beside space and and tab like other shells


        Even / sometimes separates command arguments like dir/b/c/d or yourcommand/param but luckily it doesn't apply in this case. So 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' will be passed as multiple parameters if there are any word separator in it, which is , in this case, and the command will be equivalent to



        perl  -pe  's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2   $3-$4-$5   $6   $   & /'


        and your proposed command dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv will be parsed as



        >>filename.csv >perl dir -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2  $3-$4-$5  $6  $ & /'


        since >file redirection can appear at any places. That causes the Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg" you saw above, because that's not a valid switch to dir



        That's also the reason why there's an error from perl Can't find string terminator "'" anywhere before EOF at -e line 1 apart from the lines from cmd because in the first parameter begins with ' but there's no closing '. It's easy to check how it's split into parameters:



        E:>type testparam.bat
        @echo off
        :loop
        if "%1"=="" goto :exit
        echo "%1"
        shift
        goto :loop
        :exit

        E:>testparam -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
        "-pe"
        "'s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2"
        "$3-$4-$5"
        "$6"
        "$"
        '/'' is not recognized as an internal or external command,
        operable program or batch file.


        Now look at the output and notice the funny things happen: because of the ampersand, &/' will end the previous command and execute a command named /' which doesn't exist and outputs an error like above





        The best solution is to use PowerShell, where you can run the command with just a slight change because single quote is also used for quoting in PowerShell



        ls |% { $_.Name } | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


        ls along with dir and gci in PowerShell alias to Get-ChildItem. And you need |% (which is an alias of ForEach-Object) to print the object names. An alternative in PowerShell is



        cmd /c dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


        I've left out the redirection for clarity. The output will be printed to stdout. If you want to redirect it to file just append >filename.csv or >>filename.csv depending on whether you want to overwrite or append



        Running the command in cmd would be far more troublesome (and new code should use PowerShell anyway, as it has bene the default options for a long time)



        dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2^,$3-$4-$5^,$6^,$^&^/'


        In this case changing ' to " also works, but it'll quickly break in many other situations



        dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/"




        Note that even in Linux your command is also wrong



        ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


        You need >filename.csv instead of . filename.csv






        share|improve this answer















        The other answer is not correct. If you do that you'll still get errors when running in cmd





        C:Users>dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
        Can't find string terminator "'" anywhere before EOF at -e line 1.
        '/'' is not recognized as an internal or external command,
        operable program or batch file.


        The reason is due to multiple issues in the command




        • Single quote ' is not a quoting symbol in cmd.exe


        • Comma ,, semicolon ; and equals = are also delimiters beside space and and tab like other shells


        Even / sometimes separates command arguments like dir/b/c/d or yourcommand/param but luckily it doesn't apply in this case. So 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' will be passed as multiple parameters if there are any word separator in it, which is , in this case, and the command will be equivalent to



        perl  -pe  's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2   $3-$4-$5   $6   $   & /'


        and your proposed command dir > perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' >> filename.csv will be parsed as



        >>filename.csv >perl dir -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2  $3-$4-$5  $6  $ & /'


        since >file redirection can appear at any places. That causes the Invalid switch - "(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg" you saw above, because that's not a valid switch to dir



        That's also the reason why there's an error from perl Can't find string terminator "'" anywhere before EOF at -e line 1 apart from the lines from cmd because in the first parameter begins with ' but there's no closing '. It's easy to check how it's split into parameters:



        E:>type testparam.bat
        @echo off
        :loop
        if "%1"=="" goto :exit
        echo "%1"
        shift
        goto :loop
        :exit

        E:>testparam -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'
        "-pe"
        "'s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2"
        "$3-$4-$5"
        "$6"
        "$"
        '/'' is not recognized as an internal or external command,
        operable program or batch file.


        Now look at the output and notice the funny things happen: because of the ampersand, &/' will end the previous command and execute a command named /' which doesn't exist and outputs an error like above





        The best solution is to use PowerShell, where you can run the command with just a slight change because single quote is also used for quoting in PowerShell



        ls |% { $_.Name } | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


        ls along with dir and gci in PowerShell alias to Get-ChildItem. And you need |% (which is an alias of ForEach-Object) to print the object names. An alternative in PowerShell is



        cmd /c dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/'


        I've left out the redirection for clarity. The output will be printed to stdout. If you want to redirect it to file just append >filename.csv or >>filename.csv depending on whether you want to overwrite or append



        Running the command in cmd would be far more troublesome (and new code should use PowerShell anyway, as it has bene the default options for a long time)



        dir | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2^,$3-$4-$5^,$6^,$^&^/'


        In this case changing ' to " also works, but it'll quickly break in many other situations



        dir | perl -pe "s/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/"




        Note that even in Linux your command is also wrong



        ls | perl -pe 's/(.)(.*)_(d{4})(d{2})(d{2})_(d+).jpg/u$1$2,$3-$4-$5,$6,$&/' . filename.csv


        You need >filename.csv instead of . filename.csv







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 13 at 2:31

























        answered Feb 12 at 11:24









        phuclvphuclv

        10.5k64295




        10.5k64295






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1404656%2fwhy-is-this-perl-command-failing-on-the-windows-command-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?