V
V
Vladislav2018-06-13 17:02:02
cmd/bat
Vladislav, 2018-06-13 17:02:02

BAT. How to copy a part of a file starting from a certain line?

Data.txt

Вступление.
На несколько строк.

Заголовок нужных данных
Иванов
Петров
Сидоров
Васечкин

Result.txt
Иванов
Петров
Сидоров
Васечкин

There are text files of unknown size, the required information is at the end. It is preceded by a known string, for example, "Header of the required data". You need all the lines after the specified one (or together with it - it doesn’t matter) and append to the end of another file.
One of the possible solutions, but not complete. A working example from some amel27 to display the next line after the data start separator. I tried to repeat the last line increasing the counter and faced the fact that the script does not stop when it reaches the end of the file.
@echo off
set file=1.txt
set word=искомое слово

for /f "tokens=1  delims=[]" %%n in ('find /i /n "%word%" ^<"%file%"') do set /a "$n=%%n+1"
for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^<"%file%"^|findstr /b "\[%$n%\]"') do echo.%%b

My non-working version "on the forehead"
for /f "tokens=1  delims=[]" %%n in ('find /i /n "%word%" ^<"%file%"') do set /a "$n=%%n"
:doagain
set $flag1=1
set /a $n=%$n%+1
for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^<"%file%"^|findstr /b "\[%$n%\]"') do (echo.%%b & set $flag1=2)
if $flag1 leq 2 goto doagain

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-06-13
@Sabin

Added a little:

@echo off
set "file=1.txt"
set "search=Заголовок нужных данных"
set "header_line="
for /f "tokens=1  delims=[]" %%a in ('find /i /n "%search%" "%file%" ') do set "header_line=%%a"
if defined header_line (
  more +%header_line% "%file%"
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question