0

Need help creating a batch that displays a certain amount of text (e.g 5 lines of text) from a txt file but only below a specific key word e.g 'Home' and finally removing any duplicate text

So ,

Search for specific string e.g 'Home' any text below ‘home’ display not all just 5 lines worth and finally remove any duplicate sentence’s

I've tried modifying the following command .

@echo OFF

:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (data.txt) do (
set /a LINES=LINES+1
)

:: Print the last 10 lines (suggestion to use more courtsey of dmityugov)
set /a LINES=LINES-10
more +%LINES% < data.txt

Displaying lines from text file in a batch file

Read every 5th line using Batch Script

I don't know if its possible to remove duplicates

Update

Yes that right duplicate lines within the block of 5 following the keyword

However Don't worry about removing duplicates my main concern is just trying to show text below a certain string e.g Home

I have the below command but doesn't show all information below the text just one line ideally I would like to adjust the amount displayed e.g 5 lines worth of data

setlocal EnableDelayedExpansion
rem Assemble the list of line numbers
set numbers=
set "folder=C:\test\world.txt"
for /F "delims=:" %%a in ('findstr /I /N /C:"home" "%folder%"') do (
set /A before=%%a-0, after=%%a+3
set "numbers=!numbers!!before!: !after!: "
)
rem Search for the lines
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%folder%" ^| findstr /B  "%numbers%"') do echo. %%b)

batch script to print previous and next lines of search string in a text file

Community
  • 1
  • 1
Elixir
  • 303
  • 3
  • 9
  • 26
  • 1
    What do you mean by "duplicates?" Are these duplicate lines within the block of 5 following the keyword, or any line of those 5 that has a duplicate anywhere else in the file, or has a duplicate within the block(s) alreay shown? Do you want to show 5 lines (except duplicated text) or what's left of the 5 lines after the duplicated text is removed? Do you want to display the line containing the target string? Is the line containing the target counted as line 1 of the 5? Editing-in an exaple would be helpful. – Magoo Jul 18 '14 at 00:12
  • Hi Magoo I've updated the question – Elixir Jul 18 '14 at 08:27

1 Answers1

1
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $
For %%b IN ($) DO FOR  /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
SET /a before=0
SET /a after=5
SET "target=home"
SET /a count=0
SET "file=q24813694.txt"
FOR /f "delims=:" %%a IN ('findstr /i /n /L /c:"%target%" "%file%"'
 ) DO SET /a $!count!=%%a-%before%&SET /a count+=1
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "." "%file%"') DO (
 SET "printed="
 FOR /f "tokens=1,2delims==" %%m IN ('set $ 2^>nul') DO IF NOT DEFINED printed IF %%a geq %%n (
  SET /a count=%%n+%before%+%after%
  IF %%a geq !count! (SET "%%m=") ELSE (SET "printed=Y"&ECHO %%b)
 )
)

GOTO :EOF

This routine should do the trick. You'd need to set file to suit yourself, of course; and to set the target.

If you want to set the number of lines before to print, and those after (which includes the target line) then those should work, too.

Magoo
  • 77,302
  • 8
  • 62
  • 84