【问题标题】:Batch file to output last line of findstr批处理文件以输出 findstr 的最后一行
【发布时间】:2014-01-08 21:59:31
【问题描述】:

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

@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

【问题讨论】:

    标签: batch-file tail findstr


    【解决方案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
    

    【讨论】:

    • 谢谢 - 这就像一个魅力。将 findstr 的输入更改为我的 txt 文件。
    【解决方案2】:
    setLocal enableDelayedExpansion
    for /f %%a in (computers.txt) do for /f "tokens=*" %%A in ('findstr /xs "%%a"') do set lastFound=%%A
    echo !lastFound!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多