How can I find a line in a file and copy the found line and the next line to another file?

I have 2 lines in a file named database.txt

. One line contains the question and the next line below contains the answer.

For example:

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

      

If the user is looking for When, India, Independence? , then the first line is copied to another file named result.txt

. But I want both lines to be copied, Q&A.

Can anyone shed some light here?

+3


source to share


2 answers


Help section What topics can you ask here? should always read first before posting a question. The Stack Overflow community is not meant to do the entire programming assignment for others. It looks like you haven't put any effort into coding the batch file for your task yourself.

But you're in luck as I was interested in finding one or more lines in a text file and copying the lines found, as well as one or more lines below each line found, into another text file using only standard Windows commands and applications.

I usually accomplish such tasks with the UltraEdit text editor with its powerful Perl regex engine and scripting support, and would never have come up with the idea of ​​using a batch file to grab lines from a text file and store them in another text file.

Here is a generic batch file solution with comments explained to copy the lines found and 2 to N (see LineCount) consecutive lines below each line found 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 another uncommented batch file, optimized to copy only every line found and below line 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
)

      

+3


source


The helper batch file used below uses native batch processing techniques and does not require any third party tools downloaded.

Search terms can be organized and ordered in different ways if you describe how they will be used.

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

      



For this, an auxiliary batch file findrepl.bat

(by aacini) is used - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place findrepl.bat

in the same folder as the batch file or path.

0


source







All Articles