如何在批次腳本中搜尋並提取部分字串?

如何在批次腳本中搜尋並提取部分字串?

我對 Windows 批次腳本非常陌生,無法從文字檔案中搜尋和提取字串的一部分並將其顯示出來。一些樣本數據如下所示。

搜尋的關鍵字是根據學號,例如:STUD777012

如果您能提供協助,我們將不勝感激。

非常感謝。


預期輸出範例:

STUD777012,回傳代碼:0,分析未偵測到錯誤

STUD777293,回傳代碼:4,分析偵測到警告

STUD777086,回傳代碼:8,分析偵測到錯誤

STUD777099,回傳代碼:0,分析未偵測到錯誤


日誌資料範例:

Compiling STUD777012 to Data Structure 
This is prg version 380.10.20 
This is StudPrg.exe version 6.24 
debug enabled version
StudPrg.exe finished 
prg finished with return code: 0
status:
  Analysis detected no errors

Compiling STUD777293 to Data Structure 
This is prg version 380.10.20 
This is StudPrg.exe version 6.24 
debug enabled version
StudPrg.exe finished
This is StudPrg.exe version 6.24 
debug enabled version
StudPrg.exe finished  
prg finished with return code: 4
status:
  Analysis detected warnings

Compiling STUD777086 to Data Structure 
This is prg version 380.10.20 
This is StudPrg.exe version 6.24 
debug enabled version
StudPrg.exe finished  
This is  StudPrg.exe version 6.24 
debug enabled version
StudPrg.exe finished  
prg finished with return code: 8
status:
  Analysis detected errors

答案1

如果您開啟文件記事本++並進行正規表示式替換(Ctrl+H):

Find what: Compiling (\w+).*?(return code[^\r\n]*).*?(analysis detected.*?)(\n|$)
Replace with: \1 \2 \3

你應該得到你想要的。確保您啟用了正規表示式並且 '.匹配新行'設定。例如

在此輸入影像描述

相關內容