print lines after a specific word in text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Amin
Posts: 20
Joined: 30 May 2011 05:25

print lines after a specific word in text file

#1 Post by Amin » 13 Aug 2012 05:02

Hi All;
I want to print 2 lines after each occurrence of some word in a text file:

example file:
----------------------------
text1 text2 text3 ...
WORD1
linex
liney

any text
WORD2
linez
linew
any text2

-------------------------------

i want to have the following in the output:

WORD1
linex
liney

WORD2
linez
linew


Generaly speaking: search is based on the occurrence of a pecial string in the file but printing the lines before or after the string !

Thanks

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: print lines after a specific word in text file

#2 Post by abc0502 » 13 Aug 2012 09:02

Can you make that more clear, i don't get what you want to do.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: print lines after a specific word in text file

#3 Post by dbenham » 13 Aug 2012 10:03

Here is a very simple solution that works well with small files.

Code: Select all

@echo off
setlocal enableDelayedExpansion

set "word=WORD"
set "keep=3"
set "file=test.txt"

set n=%keep%
for /f "usebackq delims=" %%A in ("%file%") do (
  set "ln=%%A"
  if "!ln:%word%=!" neq "!ln!" (
    echo(
    set n=0
  )
  if !n! lss !keep! (
    echo !ln!
    set /a n+=1
  )
)

But the above code has the following limitations:
- Blank lines are ignored
- Lines beginning with ; are ignored
- Lines containing ! are corrupted
- The search is case insensitive
- The WORD cannot contain = or !
- The WORD cannot start with * or ~
- Lines must be less than ~8191 bytes long
- It is slow if the WORD appears infrequently within a large file

Nearly all of the problems can be fixed with various tweaks, but the basic strategy will always be slow with large files.

Here is a robust solution that overall gives good performance regardless the size of the file. The only remaining limitation is that lines must be less than ~8191 bytes long. (The line size limit decreases gradually as the number of lines increases because of the line number prefix that is added to each line)

Code: Select all

@echo off
setlocal disableDelayedExpansion

set "word=WORD"
set "keep=2"
set "file=test.txt"

set "tempBase=%temp%\search%random%"

setlocal enableDelayedExpansion
>"%tempBase%search" echo !word:\=\\!
>"%tempBase%keepers" (
  for /f "delims=:" %%A in ('findstr /ng:"%tempBase%search" "%file%"') do (
    for /l %%N in (0 1 %keep%) do (
      set /a "N=%%A+%%N"
      echo !N!:
    )
  )
)
endlocal

>"%tempBase%source" findstr /n "^" "%file%"

>"%tempBase%result" findstr /blg:"%tempBase%keepers" "%tempBase%source"

for /f "usebackq delims=" %%A in ("%tempBase%result") do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  echo !ln:*:=!|findstr /g:"%tempBase%search" >nul && echo(
  echo(!ln:*:=!
  endlocal
)

del "%tempbase%*"
The code is significantly faster if the blank line is not required before each line that contains WORD. The last line that contains FINDSTR can be romoved if the blank lines are not required.

Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: print lines after a specific word in text file

#4 Post by foxidrive » 13 Aug 2012 17:26

Maxfind is a small MSDOS utility that does this kind of thing well.
It does need to be used with short filenames or wildcards.

Google for:

MaxFind. Version 3.3 Copyright 1988,94 by Stan Peters



Code: Select all

mf -3 searchwords file.txt

Amin
Posts: 20
Joined: 30 May 2011 05:25

Re: print lines after a specific word in text file

#5 Post by Amin » 14 Aug 2012 00:40

Thanks Dears for your replies , the files are small , and the solutions are working .
Thanks Dave Benham for your professional and helpful replies!
:D

Post Reply