如何从文本中提取使用一批又文本的特定行。 [英] how to extract specific lines from text to another text using batch.

查看:175
本文介绍了如何从文本中提取使用一批又文本的特定行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与各种IP地址的ping我的日志文件的例子。

Example of my log file with pings on various IP addresses.

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地址。然后在另一个文件打印出来的线条。

I want to get all the lines with Request timed out including the IP address I pinged. Then print out the lines in another file.

不知道如何去吗?

有一个code,我发现。唯一的问题是,结果显示3多余的线条。
code:

there is a code I found. the only problem was the results showed 3 extra lines. code:

@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 因为这控制台应用程序的目的不是寻找块不能在这里工作。它是专为在一条线上,输出查找字符串包含字符串发现行。

The approach with FINDSTR can't work here as this console application is not designed for finding blocks. It is designed for finding a string in a line and output the line containing the found string.

下面是该任务评论批处理文件:

Here is a commented batch file for this task:

@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:的空白字符集NAME = %% B最好水平制表符,而不是1序列或多个空格字符作为浏览器显示根据HTML标准。

Note 1: The whitespace character in set "Name= %%B" is best a horizontal tab character and not a sequence of 1 or more space characters as the browser displays according to HTML standard.

注意2:对于一个有效的CSV文件作为结果文件,制表符作为分隔符,有必要去除标签字符集NAME = %% B 回音!IP !!名字!之间的!IP!和!名字!

Note 2: For a valid CSV file as results file with tab character as separator it would be necessary to remove the tab character in set "Name= %%B" and insert it in the line with echo !IP!!Name! between !IP! and !Name!.

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • DEL /?

  • 回声/?

  • ENDLOCAL /?

  • 退出/

  • 为/?

  • 如果/?

  • REM /?

  • 设置/?

  • SETLOCAL /?

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

这篇关于如何从文本中提取使用一批又文本的特定行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆