This works fine.
这很好用。
FOR /f "delims=" %%G in ('TagRead.exe %%L Genre') DO ( echo %GG )
But when I add a sed
command to the ('...') it fails
但是当我向('...')添加一个sed命令时,它失败了
FOR /f "delims=" %%G in (
'TagRead.exe %%L Genre ^| sed -E ''/Curtain^|Exit^|Spacer/^^!s~.*~Dance~'' '
) DO ( echo %%G)
output:
sed: newline or end of file found in pattern
EDIT: Despite that this loopless command succeeds:
编辑:尽管这个无循环命令成功:
TagRead.exe C:\temp\1.wma Genre | sed -E '/Curtain^|Exit^|Spacer/^!s~.*~Dance~'
How do I get the sed command and its quotes to be accepted in the (...) quotes?
如何在(...)引号中接受sed命令及其引号?
EDIT: dbenham said: "the single quote never needs to be escaped when used with FOR /F. The outer most single quotes delimit the command string, and any inner single quotes work just fine."
编辑:dbenham说:“当与FOR / F一起使用时,单引号永远不需要被转义。最外面的单引号分隔命令字符串,任何内部单引号都可以正常工作。”
Here's that solution:
这是解决方案:
FOR /f "delims=" %%G in (
'TagRead.exe %%L Genre ^| sed -E '/Curtain^|Exit^|Spacer/^^!s~.*~Dance~''
) DO ( echo %%G)
It fails:
The syntax of the command is incorrect.
EDIT: Here is a simplified and stand-alone version that is hopefully regarding the problem:
编辑:这是一个简化的独立版本,希望能解决这个问题:
FOR /f "delims=" %%G in (
'echo xxx ^| sed -E '/yyy/^^!s~.*~zzz~''
) DO ( echo %%G)
succeeds:
zzz
This:
echo xxx | sed -E '/yyy^|uuu/^!s~.*~zzz~'
succeeds:
zzz
This:
FOR /f "delims=" %%G in (
'echo xxx ^| sed -E ''/yyy^|uuu/^^!s~.*~zzz~'''
) DO ( echo %%G)
fails:
'uuu' is not recognized as an internal or external command, operable program or batch file.
----------- THE SOLUTION -----------
- - - - - - 解决方案 - - - - - -
The cause of failure wasn't the quotes. It was was some insufficiently escaped ^. From dbenham's 1), the minimum fix is:
失败的原因不是报价。这是一些不充分逃脱^。从dbenham的1),最低限度是:
FOR /f "delims=" %%G in (
'TagRead.exe %%L Genre ^| sed -E ''/Curtain^^^|Exit^^^|Spacer/^^!s~.*~Dance~'' '
) DO ( echo %%G)
and for the simplified version:
对于简化版本:
FOR /f "delims=" %%G in (
'echo xxx ^| sed -E ''/yyy^^^|uuu/^^!s~.*~zzz~'''
) DO ( echo %%G)
One may also remove some redundant ' :
还可以删除一些冗余的':
FOR /f "delims=" %%G in (
'TagRead.exe %%L Genre ^| sed -E '/Curtain^^^|Exit^^^|Spacer/^^!s~.*~Dance~' '
) DO ( echo %%G)
and
FOR /f "delims=" %%G in (
'echo xxx ^| sed -E '/yyy^^^|uuu/^^!s~.*~zzz~''
) DO ( echo %%G)
EDIT: The solution in an instance that is simpler still:
编辑:一个更简单的实例中的解决方案:
FOR /f "delims=" %%G in ('echo "xxx^^^|ppp" ^| find "x^^^|p"') DO ( echo %%G)
2 个解决方案
#1
2
FOR /F runs a command via a new cmd.exe process.
FOR / F通过新的cmd.exe进程运行命令。
For example:
for /f %%A in ('echo hello') do echo %%A
FOR /F runs the command using:
FOR / F使用以下命令运行命令:
C:\Windows\system32\cmd.exe /c echo hello
At the time of this posting, the desired command has still not been properly posted in the question. Let us assume the correct operating command is the following (it makes no sense to me)
在发布此消息时,仍未在问题中正确发布所需命令。让我们假设正确的操作命令如下(对我来说没有意义)
TagRead.exe C:\temp\1.wma Genre | sed -E '/Curtain^|Exit^|Spacer/^^!s~.*~Dance~'
You want to put the command in a FOR /F loop.
您想将命令放在FOR / F循环中。
You have two options.
你有两个选择。
1) You can escape all the poison characters so that they pass through the cmd.exe command properly:
1)您可以转义所有有毒字符,以便它们正确地通过cmd.exe命令:
for /f "delims=" %%G in ('TagRead.exe C:\temp\1.wma Genre ^| sed -E '/Curtain^^^|Exit^^^|Spacer/^^!s~.*~Dance~'') do echo %%G
I am guessing that you originally had !
escaped as ^^!
because you have delayed expansion enabled. The new cmd.exe process will have delayed expansion disabled, so you do not need additional escaping for this construct.
我猜你原来有!逃脱了^^!因为您已启用延迟扩展。新的cmd.exe进程将禁用延迟扩展,因此您不需要为此构造进行其他转义。
2) Or you can use the fact that CMD.EXE strips the outer most double quotes when the /C command is used. All you need to do is add double quotes :-)
2)或者您可以使用CMD.EXE在使用/ C命令时删除最外面的双引号这一事实。你需要做的就是添加双引号:-)
for /f "delims=" %%G in ('"TagRead.exe C:\temp\1.wma Genre | sed -E '/Curtain^|Exit^|Spacer/^!s~.*~Dance~'"') do echo %%G
In this case, a ^
must be removed before the !
because of how delayed expansion works: quoted escaped !
= "^!"
, and unquoted = ^^!
在这种情况下,必须先删除^!因为扩张工作有多延迟:引用逃脱! =“^!”,并且没有引用= ^^!
Of course, all of the above is contingent on whether the original command (without the loop) is valid. It looks suspect to me. Regardless, the concepts above will work with most any working command put into a FOR /F loop.
当然,以上所有都取决于原始命令(没有循环)是否有效。它看起来很可疑。无论如何,上面的概念将适用于放入FOR / F循环的大多数工作命令。
#2
1
try with usebackq
http://ss64.com/nt/for_f.html
尝试使用usebackq http://ss64.com/nt/for_f.html
for /f "usebackq delims=" %%g in (
`TagRead.exe %%L Genre ^
^| sed -E '/Curtain^|Exit^|Spacer/^^!s~.*~Dance~' `
) do ( echo %%g)
Edit: removed the doubles single quotes and keep just one.
But not sure how you can escape |
inside sed
command
编辑:删除双打单引号并保留一个。但不知道如何逃脱在sed命令中
Try to double quotes the sed
pattern in this way without usebackq
:
尝试以这种方式双引号sed模式而不使用usebackq:
for /f "delims=" %%g in (
'TagRead.exe %%L Genre ^
^| sed -E "/Curtain^|Exit^|Spacer/^^!s~.*~Dance~" '
) do ( echo %%g)
Edit:
Example with many quotes inside ()
and in command sed
replacing the string 'd'a'y'
per 'n'i'g'h't'
编辑:示例中包含许多引号()和命令sed替换字符串'd'a'y'per'n'i'g'h't'
for /f "usebackq delims=" %g in (
`echo 'd'a'y'^| sed "s/'d'a'y'/'n'i'g'h't'/"`
) do @( echo %g)
Another switching Winter\Summer
to Summer\Winter
另一个转换冬季\夏季到夏季\冬季
for /f "usebackq delims=" %g in (
`echo Winter\Summer^| sed -r "s/(.*)\\(.*)/\2\\\1/"`
) do @( echo %g)
#1
2
FOR /F runs a command via a new cmd.exe process.
FOR / F通过新的cmd.exe进程运行命令。
For example:
for /f %%A in ('echo hello') do echo %%A
FOR /F runs the command using:
FOR / F使用以下命令运行命令:
C:\Windows\system32\cmd.exe /c echo hello
At the time of this posting, the desired command has still not been properly posted in the question. Let us assume the correct operating command is the following (it makes no sense to me)
在发布此消息时,仍未在问题中正确发布所需命令。让我们假设正确的操作命令如下(对我来说没有意义)
TagRead.exe C:\temp\1.wma Genre | sed -E '/Curtain^|Exit^|Spacer/^^!s~.*~Dance~'
You want to put the command in a FOR /F loop.
您想将命令放在FOR / F循环中。
You have two options.
你有两个选择。
1) You can escape all the poison characters so that they pass through the cmd.exe command properly:
1)您可以转义所有有毒字符,以便它们正确地通过cmd.exe命令:
for /f "delims=" %%G in ('TagRead.exe C:\temp\1.wma Genre ^| sed -E '/Curtain^^^|Exit^^^|Spacer/^^!s~.*~Dance~'') do echo %%G
I am guessing that you originally had !
escaped as ^^!
because you have delayed expansion enabled. The new cmd.exe process will have delayed expansion disabled, so you do not need additional escaping for this construct.
我猜你原来有!逃脱了^^!因为您已启用延迟扩展。新的cmd.exe进程将禁用延迟扩展,因此您不需要为此构造进行其他转义。
2) Or you can use the fact that CMD.EXE strips the outer most double quotes when the /C command is used. All you need to do is add double quotes :-)
2)或者您可以使用CMD.EXE在使用/ C命令时删除最外面的双引号这一事实。你需要做的就是添加双引号:-)
for /f "delims=" %%G in ('"TagRead.exe C:\temp\1.wma Genre | sed -E '/Curtain^|Exit^|Spacer/^!s~.*~Dance~'"') do echo %%G
In this case, a ^
must be removed before the !
because of how delayed expansion works: quoted escaped !
= "^!"
, and unquoted = ^^!
在这种情况下,必须先删除^!因为扩张工作有多延迟:引用逃脱! =“^!”,并且没有引用= ^^!
Of course, all of the above is contingent on whether the original command (without the loop) is valid. It looks suspect to me. Regardless, the concepts above will work with most any working command put into a FOR /F loop.
当然,以上所有都取决于原始命令(没有循环)是否有效。它看起来很可疑。无论如何,上面的概念将适用于放入FOR / F循环的大多数工作命令。
#2
1
try with usebackq
http://ss64.com/nt/for_f.html
尝试使用usebackq http://ss64.com/nt/for_f.html
for /f "usebackq delims=" %%g in (
`TagRead.exe %%L Genre ^
^| sed -E '/Curtain^|Exit^|Spacer/^^!s~.*~Dance~' `
) do ( echo %%g)
Edit: removed the doubles single quotes and keep just one.
But not sure how you can escape |
inside sed
command
编辑:删除双打单引号并保留一个。但不知道如何逃脱在sed命令中
Try to double quotes the sed
pattern in this way without usebackq
:
尝试以这种方式双引号sed模式而不使用usebackq:
for /f "delims=" %%g in (
'TagRead.exe %%L Genre ^
^| sed -E "/Curtain^|Exit^|Spacer/^^!s~.*~Dance~" '
) do ( echo %%g)
Edit:
Example with many quotes inside ()
and in command sed
replacing the string 'd'a'y'
per 'n'i'g'h't'
编辑:示例中包含许多引号()和命令sed替换字符串'd'a'y'per'n'i'g'h't'
for /f "usebackq delims=" %g in (
`echo 'd'a'y'^| sed "s/'d'a'y'/'n'i'g'h't'/"`
) do @( echo %g)
Another switching Winter\Summer
to Summer\Winter
另一个转换冬季\夏季到夏季\冬季
for /f "usebackq delims=" %g in (
`echo Winter\Summer^| sed -r "s/(.*)\\(.*)/\2\\\1/"`
) do @( echo %g)