2012-12-11 66 views
2

需要協助找出一些命令/一個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,但我認爲我使用它錯了。

回答

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 
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