Why is this Perl command failing on the Windows command line?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
add a comment |
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
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 sos/a/b/
rather thans/............/........../
And eventually you might figure out what it is
– barlop
Feb 12 at 1:27
add a comment |
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
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
windows command-line perl
edited Feb 12 at 16:14
phuclv
10.7k64297
10.7k64297
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 sos/a/b/
rather thans/............/........../
And eventually you might figure out what it is
– barlop
Feb 12 at 1:27
add a comment |
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 sos/a/b/
rather thans/............/........../
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
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Feb 13 at 2:31
answered Feb 12 at 11:24
phuclvphuclv
10.7k64297
10.7k64297
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 thans/............/........../
And eventually you might figure out what it is– barlop
Feb 12 at 1:27