批处理文件输出findstr的最后一行

时间:2014-01-08 21:59:31

标签: batch-file tail findstr

我正在尝试在文件夹中的文件中找到一个机器列表,并打印出输出的最后一行。

@echo off
for /f %%a in (computers.txt) do findstr /xs "%%a" unhealthy.txt
pause

computers.txt文件包含300台计算机的列表。

我希望它只输出它找到的每个实例的最后一行。

现在,该命令显示并输出计算机名称的所有实例,而不仅仅是尾端。我试图使用" tail for Windows"但我也遇到了错误。

当前输出:

2013\10-Oct\28\unhealthy.txt:WIN57505
2013\10-Oct\29\unhealthy.txt:WIN57505
2013\10-Oct\30\unhealthy.txt:WIN57505
2013\10-Oct\31\unhealthy.txt:WIN57505
2013\11-Nov\1\unhealthy.txt:WIN57505
2013\11-Nov\4\unhealthy.txt:WIN57505
2013\11-Nov\5\unhealthy.txt:WIN57505
2013\11-Nov\6\unhealthy.txt:WIN57505

我只想要:

2013\11-Nov\6\unhealthy.txt:WIN57505

2 个答案:

答案 0 :(得分:0)

@echo off
setlocal enableextensions disabledelayedexpansion
for /f %%a in (computers.txt) do (
    set "line="
    for /f "tokens=*" %%b in ('findstr /xs "%%a" *') do set "line=%%b"
    setlocal enabledelayedexpansion
    echo(!line!
    endlocal
)
pause
endlocal

答案 1 :(得分:0)

setLocal enableDelayedExpansion
for /f %%a in (computers.txt) do for /f "tokens=*" %%A in ('findstr /xs "%%a"') do set lastFound=%%A
echo !lastFound!
相关问题