2016-05-12 19 views
2

さまざまな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アドレスを含むRequest timed outを持つすべての行を取得したいです。その後、別のファイルに行を印刷します。

どのようにすればいいですか?

コードが見つかりました。唯一の問題は、結果に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 

はどこでも私が間違っていましたか?

+0

なぜあなたは道によって、あなたがいないよ( 'for'ループを通る'%ログ% 'を実行しています。 '%log%'の周りに括弧がありません)?あなたはまったくそれをする必要はありません。単に 'findstr/C:" Request "%log%>>%test%' – SomethingDark

+0

は、ログファイルの代わりに 'ping'出力を直接解析する方がはるかに簡単です。たとえば、[この回答](http://stackoverflow.com/a/24965777/2152082) – Stephan

+0

を参照@SomethingDarkあなたの返信に感謝します。私はコードを実行した。それは私にその言葉を含む行だけを与えました。私も回線のIPアドレスが必要です。助けてくれてありがとう! –

答えて

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"内の空白文字は、水平タブ文字ではなく、ブラウザなどの1つの以上の空白文字の列が最適ですHTML標準に従って表示します。

注2:有効なCSVファイルの場合セパレータとしてタブ文字を使用して結果ファイルとしてset "Name= %%B"にタブ文字を削除し、!IP!!Name!echo !IP!!Name!に沿って、それを挿入することが必要であろう。

使用されているコマンドとその動作方法を理解するには、コマンドプロンプトウィンドウを開き、次のコマンドを実行して、コマンドごとに表示されているすべてのヘルプページをすべてよく読んでください。

  • del /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
関連する問題