0

I've been having some problem in using FIND or FINDSTR command to find a particular string in a log file and return its output with the matching string along with 1 line above and 1 line below the matching line.

So far, this is the only command that I've tried. I've tried to search online, but couldn't find anything useful.

findstr /n "NETWORK ISSUE DETECTED" c:\Log.txt

Sample log data (Log.txt):

1371524155  Tue Jun 18 10:55:55 2013
1371524160  Tue Jun 18 10:56:00 2013
1371524165  Tue Jun 18 10:56:05 2013
NETWORK ISSUE DETECTED
 1371523243  Tue Jun 18 10:40:43 2013
1371523248  Tue Jun 18 10:40:48 2013
1371523253  Tue Jun 18 10:40:53 2013

Desire output:

1371524165  Tue Jun 18 10:56:05 2013
NETWORK ISSUE DETECTED
 1371523243  Tue Jun 18 10:40:43 2013

Appreciate for your kind help.

Thank you.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Peter
  • 1
  • 1
  • 4
  • Hi, anybody out there that could help me out? Thanks :) – Peter Jun 19 '13 at 07:11
  • I was looking for the same thing....Except I wanted the next 10 lines from the issue. I will try adding DOS a tag to see if that helps. – JeffJak Jul 02 '13 at 17:49
  • I don't see any information in the `findstr` documentation that says this is possible, nor do I see it [here](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/findstr.mspx?mfr=true). The fact that there's nothing saying `findstr` supports this functionality might be the reason you're not getting answers here. – Ken White Jul 02 '13 at 18:07

1 Answers1

0

old question, but I just found it.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1 delims=[]" %%i in ('type test.txt^|find /n "NETWORK ISSUE DETECTED"') do (
  set /a a=%%i-1
  set /a b=%%i+1
  for /f "tokens=1,* delims=[]" %%j in ('find /n /v "" test.txt^|findstr /l "[!a!] [%%i] [!b!]"') do echo %%k 
  echo/
)

the way it works: numbering the lines, find the lines "NETWORK ISSUE DETECTED" and their line numbers; find and print the lines [linenumber-1], [linenumber], [linenumber+1]

Stephan
  • 53,940
  • 10
  • 58
  • 91