繁体   English   中英

如何在批处理脚本中搜索和提取部分字符串

[英]How to search and extract part of string in batch script

我对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
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
FOR /f "tokens=1,2,6" %%a IN (q25290636.txt) DO (
 IF "%%a"=="Compiling" SET "stud=%%b"
 IF "%%a"=="prg" (
  IF "%%c"=="0" ECHO !stud! return code %%c, Analysis detected no errors
  IF "%%c"=="4" ECHO !stud! return code %%c, Analysis detected warnings
  IF "%%c"=="8" ECHO !stud! return code %%c, Analysis detected errors
 )
)

GOTO :EOF

假设将错误代码编号转换为文本是安全的(即使看起来是多余的)

我使用了一个名为q25290636.txt的文件, q25290636.txt包含用于测试的数据。

您未提供STUD777099的数据,返回码:0,分析未检测到错误


修订以隔离单项记录。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "target=STUD%~1"
FOR /f "tokens=1,2,6" %%a IN (q25290636.txt) DO (
 IF "%%a"=="Compiling" SET "stud=%%b"
 IF "%%a"=="prg" (
  ECHO !stud!|FINDSTR /L /B "%target%" >NUL
  IF NOT ERRORLEVEL 1 (
   IF "%%c"=="0" ECHO !stud! return code %%c, Analysis detected no errors
   IF "%%c"=="4" ECHO !stud! return code %%c, Analysis detected warnings
   IF "%%c"=="8" ECHO !stud! return code %%c, Analysis detected errors
  )
 )
)

GOTO :EOF

要针对所有目标运行,请不带参数运行。

要为STUD777293运行,只需运行此批次 777293

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM