【问题标题】:how to extract specific lines from text to another text using batch.如何使用批处理从文本中提取特定行到另一个文本。
【发布时间】:2016-05-12 02:35:07
【问题描述】:

我的日志文件示例,其中包含对各种 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

我哪里做错了?

【问题讨论】:

  • 你为什么要通过for 循环运行%log%(顺便说一句,你没有;你缺少%log% 周围的括号)?你根本不需要这样做。只需findstr /C:"Request" %log% >>%test%
  • 可能更容易直接解析ping 输出而不是日志文件。参见例如this answer
  • @SomethingDark 感谢您的回复。我运行了代码。它只给了我包含这个词的行。我也需要线路的IP地址。感谢您的帮助!
  • @Mofi 感谢您的回答和帮助。感谢您帮助我了解如何逐行处理。但我运行的代码只给了我 IP 地址。
  • @ZedekiahTeo 我的代码仅将request timed out 出现至少 3 次的 IP 地址(带或不带名称)输出到结果文件中。设计成功的 IP 地址(和名称)不包括在内,因为根据最初的问题,我认为没有兴趣。当然,可以调整代码以将所有 IP 地址与状态信息(已回复与未回复)输出到结果文件中。随意将代码调整为您想要的输出格式。

标签: batch-file command-prompt


【解决方案1】:

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" 中的制表符并将其插入到 echo !IP!!Name! 之间的行中!IP!!Name!

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

  • del /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多