如何使用批处理从文本中提取特定行到另一个文本。

时间:2016-05-12 02:35:07

标签: batch-file command-prompt

我的日志文件示例,其中包含各种IP地址的ping。

Pinging 10.62.36.161 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 10.62.36.161:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

================================================= 

Pinging src.g03.yahoodns.net [98.137.236.150] with 32 bytes of data:
Reply from 98.137.236.150: bytes=32 time=203ms TTL=43
Reply from 98.137.236.150: bytes=32 time=204ms TTL=45
Reply from 98.137.236.150: bytes=32 time=192ms TTL=43
Reply from 98.137.236.150: bytes=32 time=211ms TTL=43

Ping statistics for 98.137.236.150:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 192ms, Maximum = 211ms, Average = 202ms

我希望获得Request timed out的所有行,包括我ping的IP地址。然后打印出另一个文件中的行。

知道怎么做到这一点吗?

我找到了一个代码。唯一的问题是结果显示3条额外的线条。 代码:

@echo off
set log=C:\Users\"name"\Desktop\log.txt
set test=C:\Users\"name"\Desktop\test.txt

setlocal EnableDelayedExpansion
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"Request" %log%') do (
set /A before=%%a-1, after=%%a+1
set "numbers=!numbers!!before!: !after!: "
)
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %log% ^| findstr /B "%numbers%"') do  echo %%b) > %test%
pause

我得到的结果:

Pinging 10.62.36.161 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
ECHO is off.
Reply from 8.8.8.8: bytes=32 time=6ms TTL=49
Reply from 8.8.8.8: bytes=32 time=5ms TTL=49

我做错了什么?

1 个答案:

答案 0 :(得分:2)

使用 FINDSTR 的方法无法在此处运行,因为此控制台应用程序不是为查找块而设计的。它用于在一行中查找字符串并输出包含找到的字符串的行。

以下是此任务的注释批处理文件:

@echo off
setlocal EnableDelayedExpansion
set "LogFile=%USERPROFILE%\Desktop\log.txt"
set "ResultsFile=%USERPROFILE%\Desktop\results.txt"

rem Exit this batch file if the log file does not exist.
if not exist "%LogFile%" exit /B

rem Delete the results file if existing for a previous run.
if exist "%ResultsFile%" del /F "%ResultsFile%"

rem Process the log file line by line with assigned the first
rem three space or tab separated strings of each line to the
rem loop variables A, B and C for further investigation.
for /F "usebackq tokens=1-3" %%A in ("%LogFile%") do (

    rem Is the first string on the line the word "Pinging"?
    if "%%A" == "Pinging" (
        set "Count=0"
        rem Is the third string "with"?
        if "%%C" == "with" (
            rem The second string is the IP address and no name.
            set "Name="
            set "IP=%%B"
        ) else (
            rem The second string is the name and the third string is the
            rem IP address enclosed in square brackets which are removed.
            set "Name=  %%B"
            set "IP=%%C"
            set "IP=!IP:~1,-1!"
        )
    ) else if "%%A %%B %%C" == "Request timed out." (
        rem This is a line with information that the request timed out.
        rem Increment count and if it reaches 3, then reaching this IP
        rem failed three times and IP and name are written into results file.
        set /A Count+=1
        if !Count! == 3 echo !IP!!Name!>>"%ResultsFile%"
    )
)

endlocal

注1: set "Name= %%B"中的空格字符最好是水平制表符,而不是浏览器根据HTML标准显示的1个或多个空格字符的序列。

注2:对于有效的CSV文件作为带有制表符作为分隔符的结果文件,有必要删除set "Name= %%B"中的制表符并将其插入{{1}行中1}}在echo !IP!!Name!!IP!之间。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • !Name!
  • del /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • if /?
  • rem /?
  • set /?
相关问题