Batch file to output last line of findstr

不打扰是莪最后的温柔 提交于 2019-12-04 05:26:11

问题


I am trying to find a list of machines in files in folders, and print out the last line of the output only.

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

The computers.txt file has a list of 300 machines.

I want that to output only the last line of each instance it finds.

Right now the command displays and outputs all instances of the computer name, not just the tail end. I've tried to use "tail for Windows" but am getting errors as well.

Current output:

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

I only want:

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


回答1:


@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



回答2:


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


来源:https://stackoverflow.com/questions/21007472/batch-file-to-output-last-line-of-findstr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!