感谢。
我有一个日志文件,其中包含命令结果,如(模式)
----- Opening connection to: 10.4.2.1 at 13:35:07.49 -----
Passed
----- Closing connection to: 10.4.2.1 at 13:35:26.54 -----
----- Opening connection to: 10.4.28.17 at 13:35:07.49 -----
Failed
----- Closing connection to: 10.4.28.17 at 13:35:45.50 -----
----- Opening connection to: 10.4.24.15 at 13:35:07.49 -----
----- Closing connection to: 10.4.24.15 at 13:36:27.85 -----
----- Opening connection to: 10.4.4.5 at 13:35:07.49 -----
----- Closing connection to: 10.4.4.5 at 13:36:27.85 -----
我试图获取在(打开/关闭连接到模式)
之间有空行的服务器列表到目前为止我能够做到只获得结果文件的最后一个条目
代码:
@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
color 9E
title Tool
SET "sourcedir=C:\Logs\"
SET "destdir=C:\Logs\"
for /f "tokens=1 delims=[]" %%a in ('find /n "Opening connection to"^<"%sourcedir%\Results.log" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "Closing connection to"^<"%sourcedir%\Results.log" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%sourcedir%\Results.log" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%destdir%\FailedList.log"
GOTO :EOF
所以在FailedList.log中,我应该只看到ip详细信息
10.4.24.15
10.4.4.5
答案 0 :(得分:2)
当输入数据具有示例数据的模式时,此代码将获得所需的输出:
@echo off
setlocal EnableDelayedExpansion
set "ip="
(for /F "tokens=1,5" %%a in (Results.log) do (
if "%%b" equ "!ip!" echo !ip!
set "ip=%%b"
)) > FailedList.log