命令行帮助清理和代码生成

时间:2012-12-11 04:01:57

标签: batch-file command-line sed

需要帮助找到一些bat文件的命令/工具来自动转换内容:

%PCI\VEN_1B21&DEV_0601.DeviceDesc%= asahci_Inst, PCI\VEN_1B21&DEV_0601
%PCI\VEN_1B21&DEV_0602.DeviceDesc%= asahci_Inst, PCI\VEN_1B21&DEV_0602
%PCI\VEN_1B21&DEV_0611.DeviceDesc%= asahci_Inst, PCI\VEN_1B21&DEV_0611
%PCI\VEN_1B21&DEV_0612.DeviceDesc%= asahci_Inst, PCI\VEN_1B21&DEV_0612

到此(清理):

VEN_1B21&DEV_0601
VEN_1B21&DEV_0602
VEN_1B21&DEV_0611
VEN_1B21&DEV_0612

对此(插入代码和下一行):

devcon find * | find /i "VEN_1B21&DEV_0601" >NUL 
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED
devcon find * | find /i "VEN_1B21&DEV_0602" >NUL
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED
devcon find * | find /i "VEN_1B21&DEV_0611" >NUL
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED
devcon find * | find /i "VEN_1B21&DEV_0612" >NUL
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED

我尝试了各种工具,如rxfind(零文档)和sed,但我认为我使用它错了。

2 个答案:

答案 0 :(得分:1)

使用sed:

pre='devcon find * | find \/i "'
post='" >NUL\nif \/i %ERRORLEVEL% equ 0 set DEVICE=DETECTED'
sed -n "s/.*\\\\\(.*\)/$pre\\1$post/p" input

或:

sed 's/.*\\\(.*\)/devcon find * | find \/i "\1" >NUL\nif \/i %ERRORLEVEL% equ 0 set DEVICE=DETECTED/' input

答案 1 :(得分:0)

您也可以使用awk

awk -F '\' -v a="devcon find * | find /i" -v b=">NUL\nif /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED" '{ print a, "\""$NF"\"", b }' file

或:

awk -F '\' '{ printf "devcon find * | find /i \"%s\" >NUL\nif /i %%ERRORLEVEL%% equ 0 set DEVICE=DETECTED\n", $NF }' file

结果:

devcon find * | find /i "VEN_1B21&DEV_0601" >NUL
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED
devcon find * | find /i "VEN_1B21&DEV_0602" >NUL
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED
devcon find * | find /i "VEN_1B21&DEV_0611" >NUL
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED
devcon find * | find /i "VEN_1B21&DEV_0612" >NUL
if /i %ERRORLEVEL% equ 0 set DEVICE=DETECTED