Hi everyone
I have a file.txt like this:
------------------------------------------------------------------------------------------------------
sadjasjdakdjka
sajdasdsdaadas
sdaasd:START
aksaksas
sdasdsa
ajsda
djsjdkads:END
asdjjs
asjdsajd
ajsdsjkd
asjsa:START
salsdjjd
sdjlsj
kdkk
kdkkd
kdkdkd
k
&
sjdskjd:END
skdalks
sdajsd
saojd: START
sdjkajsa
sjksajd
jksdjs
Sddklsaj:END
------------------------------------------------------------------------------
I need to extract the lines between START and END and save them in a file .txt
My output should be
-------------------------------------------------------------------------------
sdaasd:START
aksaksas
sdasdsa
ajsda
djsjdkads:END
asjsa:START
salsdjjd
sdjlsj
kdkk
kdkkd
kdkdkd
k
&
sjdskjd:END
saojd: START
sdjkajsa
sjksajd
jksdjs
Sddklsaj:END
---------------------------------------------------------------
I need do this using a code in .bat file.
Could you help me?
I tried this code:
____________________________________________
for /f "tokens=1 delims=[]" %%a in ('find /n "START" ^< C:\Users\Antonio\Desktop\dev.txt') do (
set H=%%a
)
for /f "tokens=1 delims=[]" %%a in ('find /n "END" ^< C:\Users\Antonio\Desktop\dev.txt') do (
set T=%%a
)
for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< C:\Users\Antonio\Desktop\dev.txt') do (
if %%a geq !H! if %%a leq !T! echo=%%b
)>> C:\Users\Antonio\Desktop\dev_1.txt
)
____________________________________________________________-
but it gives me only the lines between the last couple START and END
How to get lines between two strings and to put the results in a .txt files
Moderator: DosItHelp
Re: How to get lines between two strings and to put the results in a .txt files
Assumed that teh input file is valid (== no unexpected start or end of any block, as shown in your sample file), this should help you:
penpen
Code: Select all
@echo off
setlocal enableExtensions enableDelayedExpansion
set "source=C:\Users\Antonio\Desktop\dev.txt"
set "target=C:\Users\Antonio\Desktop\dev_1.txt"
set "source=Z:\dev.txt"
set "target=dev_1.txt"
set "include=lines.tmp.txt"
set "source_n=source_n.tmp.txt"
> "%include%" (
set "line="
for /f "tokens=1 delims=:" %%a in ('findstr /n "START END" "%source%"') do (
if defined line (
for /l %%c in (!line!, 1, %%~a) do (
echo(%%~c:
)
set "line="
) else (
set "line=%%~a"
)
)
)
> "%source_n%" findstr /n "^" "%source%"
setlocal enableExtensions disableDelayedExpansion
> "%target%" (
for /f "tokens=1* delims=:" %%a in ('findstr /B /L /G:"%include%" "%source_n%"') do @echo(%%b
)
delete "%include%"
delete "%source_n%"
endlocal
endlocal
goto :eof
Re: How to get lines between two strings and to put the results in a .txt files
Perfect!!!!!!!! It works. Thank you so much for reply. Best Regard