需要一个批处理脚本来grep一个模式并设置一个变量

时间:2021-02-01 23:27:11

As I am trying to grep a pattern from a file and then need to store in a variable using batch script. Below code i am using but its not working as expected:

因为我试图从文件中grep一个模式然后需要使用批处理脚本存储在一个变量中。我正在使用下面的代码,但它没有按预期工作:

for /f %%A in (grep "USI" %1/%CNID%.log | awk '{print substr($17,5,9)}') do set "var=%%A"

1 个解决方案

#1


Changed quotes and removed the non needed grep command

更改了引号并删除了不需要的grep命令

for /f "delims=" %%A in ('
    awk "/USI/{print substr($17,5,9)}" "%1\%CNID%.log"
') do set "var=%%A"

Or, in cases where the grep is really necessary, you will need to also escape the pipe

或者,在真正需要grep的情况下,您还需要逃离管道

for /f "delims=" %%A in ('
    grep "USI" "%1\%CNID%.log" 
    ^| awk "{print substr($17,5,9)}"
') do set "var=%%A"

#1


Changed quotes and removed the non needed grep command

更改了引号并删除了不需要的grep命令

for /f "delims=" %%A in ('
    awk "/USI/{print substr($17,5,9)}" "%1\%CNID%.log"
') do set "var=%%A"

Or, in cases where the grep is really necessary, you will need to also escape the pipe

或者,在真正需要grep的情况下,您还需要逃离管道

for /f "delims=" %%A in ('
    grep "USI" "%1\%CNID%.log" 
    ^| awk "{print substr($17,5,9)}"
') do set "var=%%A"