在批处理文件的循环中使用 grep 转义字符
Escape chars with grep wihtin a loop in a batch file
我可以运行下面的代码没有问题:
echo topic = "def";ghi | grep -oP "(?<=topic = \").+?(?=\"";ghi)
但是如果我把它放在一个 .bat 文件的 for 循环中,它就不再有效了。
for /F "delims=" %%A in (urls.txt) do (
echo topic = "def";ghi | grep -oP "(?<=topic = \").+?(?=\"";ghi)
)
错误显示 .+?(? was unexpected at this time
。
这是为什么?
这是因为您的 grep
语句中的第一个 )
正在关闭 for
代码块。您要么需要将整个 grep
参数包含在引号中,要么转义任何未用脱字符号引起来的括号。
for /F "delims=" %%A in (urls.txt) do (
echo topic = "def";ghi | grep -oP "(?<=topic = \"^).+?^(?=\"";ghi^)
)
反正我觉得是对的。它可能需要一些试验和错误。但是,每当您需要在批处理脚本中转义特殊字符时,请使用 ^
.
我可以运行下面的代码没有问题:
echo topic = "def";ghi | grep -oP "(?<=topic = \").+?(?=\"";ghi)
但是如果我把它放在一个 .bat 文件的 for 循环中,它就不再有效了。
for /F "delims=" %%A in (urls.txt) do (
echo topic = "def";ghi | grep -oP "(?<=topic = \").+?(?=\"";ghi)
)
错误显示 .+?(? was unexpected at this time
。
这是为什么?
这是因为您的 grep
语句中的第一个 )
正在关闭 for
代码块。您要么需要将整个 grep
参数包含在引号中,要么转义任何未用脱字符号引起来的括号。
for /F "delims=" %%A in (urls.txt) do (
echo topic = "def";ghi | grep -oP "(?<=topic = \"^).+?^(?=\"";ghi^)
)
反正我觉得是对的。它可能需要一些试验和错误。但是,每当您需要在批处理脚本中转义特殊字符时,请使用 ^
.