如果发现特定字符串,则删除 txt 文件中的行

如果发现特定字符串,则删除 txt 文件中的行

我不知道如何使用我的批处理脚本删除特定行,因为输出是:

type: MD2
status: granted
date: 2016/03/11

type: MD2
status: detached
date: 2016/03/11

如果状态为分离,则不应在输出文件中写入/忽略孔块“类型、日期和状态”

所以我目前在做什么:

@echo off
setlocal EnableDelayedExpansion
for /f "usebackq skip=10 tokens=1,5,9 delims= " %%a in ("Log.txt") do (
    set "date=%%a"
    set "type=%%b"
    set "status=%%c"
    echo %%c|find "more">nul && set "status=not granted"
    echo %%c|find "detached" >nul && set status=""
    echo type: !type!
    echo status: !status!
    echo date: !date!
    echo.
)>>Test.txt
pause

示例 Log.txt:

2016/03/11 00:00:25:478 I LICENSESERV MD2 (Dassault Systemes V5) granted to client test (42FB1010C6B1ED82-0a292138.1)/172.1.1.1|d|d@EUROPE.S-1-5-21-1832937852-2116575123-337272265-547921.0A292138.1.GGL-42FB1010C6B1ED82|C:\Program Files\ANSYS Inc\v170\AISOL\bin\winx64\ReaderHostU.exe 
2016/03/11 00:01:28:461 I LICENSESERV MD2 (Dassault Systemes V5) detached by timeout from client test (42FB1010C6B1ED82-0a292138.1)/10.41.33.56|d|d@EUROPE.S-1-5-21-1832937852-2116575123-337272265-547921.0A292138.1.GGL-42FB1010C6B1ED82|C:\Program Files\ANSYS Inc\v

答案1

如果状态是,detached则不要写“类型、日期和状态”块

这很容易做到,使用如果命令。

代替:

echo %%c|find "more">nul && set "status=not granted"
echo %%c|find "detached" >nul && set status=""
echo type: !type!
echo status: !status!
echo date: !date!
echo.

和:

if "!status!" NEQ "detached"  (
  echo type: !type!
  echo status: !status!
  echo date: !date!
  echo.
  )

输入示例:

2016/03/11 00:00:25:478 I LICENSESERV MD2 (Dassault Systemes V5) granted to client test (42FB1010C6B1ED82-0a292138.1)/172.1.1.1|d|d@EUROPE.S-1-5-21-1832937852-2116575123-337272265-547921.0A292138.1.GGL-42FB1010C6B1ED82|C:\Program Files\ANSYS Inc\v170\AISOL\bin\winx64\ReaderHostU.exe 
2016/03/11 00:01:28:461 I LICENSESERV MD2 (Dassault Systemes V5) detached by timeout from client test (42FB1010C6B1ED82-0a292138.1)/10.41.33.56|d|d@EUROPE.S-1-5-21-1832937852-2116575123-337272265-547921.0A292138.1.GGL-42FB1010C6B1ED82|C:\Program Files\ANSYS Inc\v

示例输出:

type: MD2
status: granted
date: 2016/03/11

如果我也想排除状态,该怎么办detached;

您可以;删除!status!

代替:

set "status=%%c"

和:

set status=%%c
set status=!status:;=!

进一步阅读

相关内容