4

I have a batch script which will print the entire line of search string into a text file.

for %%i in (log.txt) do (
FINDSTR /G:pattern.txt %%i >> output.txt
)

Example: pattern.txt contains search string ERROR and below is the sample text in log.txt

2013-06-30 02:17:55,562 INFO   Service started
2013-06-30 02:17:55,578 INFO   Sending mail...
2013-06-30 02:17:55,578 DEBUG  Element value: 1
2013-06-30 02:17:55,578 ERROR  error occurred and message is ""
2013-06-30 02:17:55,578 DEBUG  bit version: 8
2013-06-30 02:17:55,578 INFO   Service stopped

The above batch script will print each line of text whenever it finds the string ERROR in log.txt So, the output.txt will look have lines like below

2013-06-30 02:17:55,578 ERROR  error occurred and message is ""

How can I print only previous and next lines of search string like below:

2013-06-30 02:17:55,578 DEBUG  Element value: 1
2013-06-30 02:17:55,578 DEBUG  bit version: 8

Thanks in advance.

Shrik
  • 65
  • 1
  • 2
  • 9

3 Answers3

4
@echo off
setlocal EnableDelayedExpansion
rem Assemble the list of line numbers
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"error occurred" log.txt') do (
   set /A before=%%a-1, after=%%a+1
   set "numbers=!numbers!!before!: !after!: "
)
rem Search for the lines
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" log.txt ^| findstr /B "%numbers%"') do echo %%b) > output.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Added two more lines to generate output.txt `rem Search for the lines for /F "tokens=1* delims=:" %%a in ('findstr /N "^" log.txt ^| findstr /B "%numbers%"') do ( if %%a == %before% >> output.txt echo %%b if %%a == %after% >> output.txt echo %%b )` Works fine for multiple search strings like "error occurred". But only prints for the last occurrence. In real-time scenario, there will be multiple occurrences of search string. Is it possible to achieve without using third party tool? – Shrik Jul 31 '13 at 06:04
  • The original code search for all occurrences of the string. Didn't you tested it? If you want to generate output.txt file, just enclose the last line this way: `(` original last line `) > output.txt`. I already modified the code above this way... – Aacini Jul 31 '13 at 15:36
  • Awesome Aacini. Sorry. The code which I modified to generate output.txt file was searching only last occurrence. Your latest code is working fine. – Shrik Aug 01 '13 at 05:49
3

This uses a helper batch file called findrepl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697

@echo off
set "var=searchterm"
type "file.txt"|findrepl "%var%" /o:-1:+1 |find /v "%var%"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
1

try this:

for /f "delims=:" %%a in ('findstr /in "error" log.txt') do set /a found=%%a
if not defined found (echo "error" not found&goto:eof)
set /a line1=found-1
set /a line2=found+1
for /f "tokens=1*delims=:" %%a in ('findstr /in "^" log.txt') do (
    if %%a==%line1% >>output.txt echo(%%b
    if %%a==%line2% >>output.txt echo(%%b
)
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Throws error: "in was unexpected at this time" for the second `for` loop `for /f "tokens=1*delims=:" in ('findstr /in "^" ATServer-webconsole.log') do (` – Shrik Jul 30 '13 at 09:29
  • Endoro missed a `%%a`: `for /f "tokens=1*delims=:" %%a in ('findstr /in "^" log.txt') do (` – Stephan Jul 30 '13 at 09:35
  • @Stephan Right. There are two more hurdles to cross. 1. It's printing only for the last occurrence of the search string. 2. Not working for search string involving two or more words like `ISSUE DETECTED`. output.txt is not generated. Shows no error. – Shrik Jul 30 '13 at 10:21
  • @Shrik sorry: made an edit. I would recommend to use `grep for Windows` for this, If you need such things more often. – Endoro Jul 30 '13 at 11:47