batch-file - 如何使用批处理将文本中的特定行提取到另一个文本。

标签 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

我想获取所有请求超时的行,包括我 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

我哪里做错了?

最佳答案

FINDSTR 方法在这里不起作用,因为此控制台应用程序不是为查找 block 而设计的。它旨在查找一行中的字符串并输出包含找到的字符串的行。

这是此任务的带注释的批处理文件:

@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" 中的空白字符最好是水平制表符,而不是 1 个或多个空格字符的序列浏览器按照HTML标准显示。

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

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

  • 删除/?
  • 回显/?
  • endlocal/?
  • 退出/?
  • 对于/?
  • 如果/?
  • 雷姆/?
  • 设置/?
  • setlocal/?

关于batch-file - 如何使用批处理将文本中的特定行提取到另一个文本。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176305/

相关文章:

batch-file - 使用批处理文件并排合并 csv 文件

windows - 如何获取包含插入符号 (^) 的密码,将其作为参数原封不动地传递给 Windows 批处理文件?

javascript - 如何从网站执行批处理文件

python - 通过 '-c' 选项将多行传递给 Python 解释器

windows - 如何在两个日期和时间时间戳之间执行算术运算(加,减)?

windows - 在指定时间后停止批处理脚本的最佳方法是什么?

mysql - 命令提示符不显示宽 MySQL 表

windows - 命令提示符 DNS 查找

python - Sublime Text 3 subl 命令在 Windows 10 中不起作用

windows - 从批处理文件中仅在日志文件中输出某些结果