Page 1 of 1

print lines after a specific word in text file

Posted: 13 Aug 2012 05:02
by Amin
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

Re: print lines after a specific word in text file

Posted: 13 Aug 2012 09:02
by abc0502
Can you make that more clear, i don't get what you want to do.

Re: print lines after a specific word in text file

Posted: 13 Aug 2012 10:03
by dbenham
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

Re: print lines after a specific word in text file

Posted: 13 Aug 2012 17:26
by foxidrive
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

Re: print lines after a specific word in text file

Posted: 14 Aug 2012 00:40
by Amin
Thanks Dears for your replies , the files are small , and the solutions are working .
Thanks Dave Benham for your professional and helpful replies!
:D