3

I have 2 lines in a file called database.txt. One line contains a question, and the consecutive line below contains the answer.

For example:

<--When did India get it's Independence?-->
India got it's independence on August 15th, 1947.

If user searches for When,India,Independence? then the 1st line is copied into another file called result.txt. But I want both lines copied, question and answer.

Can anyone throw some light here?

Mofi
  • 46,139
  • 17
  • 80
  • 143
blah
  • 103
  • 3
  • 12
  • are the lines consecutive ? – npocmaka Aug 27 '14 at 15:05
  • Question and answer both are consecutive. However there is a gap after each set. Each set has a question tag and answer like above. – blah Aug 27 '14 at 15:05
  • I edited your question for clarity. Feel free to roll back the edit if you disagree. – indiv Aug 27 '14 at 15:17
  • Haha, thanks. :) Anyone? answer? :/ – blah Aug 27 '14 at 16:18
  • this http://stackoverflow.com/questions/5631752/how-can-i-use-findstr-with-newline-regular-expression suggests it's doable, but I think you'd better off using Powershell or grep port – wmz Aug 27 '14 at 20:52
  • @wmz - grep cannot search across multiple lines, but pcregrep can. – dbenham Aug 28 '14 at 03:42
  • 1
    @dbenham Some can, as outlined here: http://stackoverflow.com/questions/3717772/regex-grep-for-multi-line-search-needed (gnuwin port seems to support all the options) - **but what's more important** multiline search is not needed here - as per OP requirement it's enough to match first line, and then print (copy) this and consecutive line, which grep can easily do (with -A) – wmz Aug 28 '14 at 08:28
  • @wmz - Sorry, of course you are correct. Thanks – dbenham Aug 28 '14 at 11:08
  • I must do it with cmd only without any 3rd party addons. That's one of the limitation. – blah Aug 28 '14 at 12:02
  • Is every question delimited by arrows like so? `<---question--->` – foxidrive Aug 30 '14 at 15:02
  • Yes @foxidrive every question is. – blah Sep 01 '14 at 04:27

2 Answers2

3

Help topic What topics can I ask about here? should be always read first before posting a question. The Stack Overflow community is not for doing the entire programming job for others. It looks like you have not made any effort on coding a batch file for your task by yourself.

But you have luck as I was interested in the challenge to find 1 or more lines in a text file and copy the found lines as well as 1 or more lines below each found line to another text file using only standard commands and applications of Windows.

I usually do such tasks with text editor UltraEdit with its powerful Perl regular expression engine and scripting support and would have never come on the idea to use a batch file for grabbing lines from a text file and store them in another text file.

Here is a general batch file solution with explaining comments for copying found lines and 2 to N (see LineCount) consecutive lines below each found line from one text file to another text file.

@echo off
setlocal EnableDelayedExpansion

rem Define the regular expression search string.
set "SearchExpression=When.*India.*Independence"

rem Define names of input and output file with full path.
set "ResultFile=C:\Temp\Result.txt"
set "SourceFile=C:\Temp\Example.txt"

rem Delete existing output file from a previous execution.
if exist "%ResultFile%" del "%ResultFile%"

rem Run a regular expression search in input file using standard Windows
rem console application FINDSTR with getting output also the line number
rem of the line with a positive match at beginning of the output line
rem and separated from the found line with a colon. Of interest in this
rem first loop is only the line number being processed in subroutine.
for /F "tokens=1 delims=:" %%N in ( '%SystemRoot%\System32\findstr.exe /R /N "%SearchExpression%" "%SourceFile%"' ) do (
    set SkipLines=%%N
    call :CopyLines
)
endlocal
goto :EOF

rem Subroutine to copy from input file the line with the positive match
rem of the regular expression search and also the next line in the file.
:CopyLines

rem Determine the number of lines to copy from input file to output file.
set LineCount=2
set SkipOption=

rem Skip all lines above the found line.
set /A SkipLines-=1

rem Option skip with value 0 results in a syntax error message.
rem Therefore define option skip only with a value greater 0.
if not "%SkipLines%" == "0" set "SkipOption=skip=%SkipLines% "

rem Copy LineCount lines starting from SkipLines+1 line to output file.
for /F "usebackq %SkipOption%delims=" %%L in ( "%SourceFile%" ) do (
   echo %%L>>"%ResultFile%"
   set /A LineCount-=1
   if "!LineCount!" == "0" goto :EOF
)

And here is one more batch file without comments optimized for copying just each found line and the line below from input to output text file.

@echo off
setlocal EnableDelayedExpansion
set "SearchExpression=When.*India.*Independence"
set "ResultFile=C:\Temp\Result.txt"
set "SourceFile=C:\Temp\Example.txt"
if exist "%ResultFile%" del "%ResultFile%"
for /F "tokens=1,2* delims=:" %%N in ( '%SystemRoot%\System32\findstr.exe /R /N "%SearchExpression%" "%SourceFile%"' ) do (
    set SkipLines=%%N
    echo %%O>>"%ResultFile%"
    call :CopyNextLine
)
endlocal
goto :EOF

:CopyNextLine
for /F "usebackq skip=%SkipLines% delims=" %%L in ( "%SourceFile%" ) do (
   echo %%L>>"%ResultFile%"
   goto :EOF
)
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • A problem with this solution is that the three search terms have to be in the same sequence as found in the source text file - for example you can't search for `India` before `When` – foxidrive Sep 01 '14 at 06:21
0

The helper batch file used below employs native batch techniques and doesn't require any downloaded third party tools.

The search terms can be organised and arranged in various ways, if you describe how this will be used.

@echo off
type "database.txt" | findrepl "^.*<--(?=.*when)(?=.*india)(?=.*independence).*-->.*$" /o:0:1 /i
pause

This uses a helper batch file called findrepl.bat (by aacini) - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place findrepl.bat in the same folder as the batch file or on the path.

foxidrive
  • 40,353
  • 10
  • 53
  • 68