Reputation: 177
The following batch is tested under win 7 x64, with GNU sed version 4.0.7
for /f "delims=" %%a in ('"%UnxUtils_path%\sed.exe" -nr "/%pattern%/ s/(^^[^,]*,)([0-9])(,.*)/\1 \3/ p" "file.txt"') do (echo %%a)
But the error occurs.
If add "call" or remove quote mark of "sed.exe" as the following, the error disappeared.
Remove the quate mark,
for /f "delims=" %%a in ('%UnxUtils_path%\sed.exe -nr "/%pattern%/ s/(^^[^,]*,)([0-9])(,.*)/\1 \3/ p" "file.txt"') do (echo %%a)
But it is not suitable for the full path of sed.exe that includes SPACE,
Add "call",
for /f "delims=" %%a in ('call "%UnxUtils_path%\sed.exe" -nr "/%pattern%/ s/(^^[^,]*,)([0-9])(,.*)/\1 \3/ p" "file.txt"') do (echo %%a)
This can be ok.
If test the command directly,
"%UnxUtils_path%\sed.exe" -nr "/%pattern%/ s/(^^[^,]*,)([0-9])(,.*)/\1 \3/ p" "file.txt"
It is ok as well.
So what is the problem of the 1st syntax?
Upvotes: 2
Views: 239
Reputation: 130919
When you run a command like for /f %a in ('someCommand') do ...
, a new command process is launched as follows (path may change): c:\Windows\system32\cmd.exe /c someCommand
.
Substituting your actual command, you get:
"%UnxUtils_path%\sed.exe" -nr "/%pattern%/ s/(^^[^,]*,)([0-9])(,.*)/\1 \3/ p" "file.txt"
If you wade through the CMD help (help cmd
or cmd /?
), you will see that CMD will strip the leading and trailing quote if the first and last characters of the command are quotes. This results in:
%UnxUtils_path%\sed.exe" -nr "/%pattern%/ s/(^^[^,]*,)([0-9])(,.*)/\1 \3/ p" "file.txt
.
You should be able to see why this fails.
This also explains why it works when you add CALL - the command no longer begins with a quote, so no quotes are stripped.
It is possible to run the command without CALL by enclosing the entire command within an extra set of quotes. However, those extra quotes should be escaped so as not to interfere with the quoting during the initial parsing phase:
for /f "delims=" %%a in ('^""%UnxUtils_path%\sed.exe" -nr "/%pattern%/ s/(^^[^,]*,)([0-9])(,.*)/\1 \3/ p" "file.txt"^"') do (echo %%a)
Upvotes: 2