How to get lines between two strings and to put the results in a .txt files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Costy
Posts: 4
Joined: 25 Feb 2018 10:43

How to get lines between two strings and to put the results in a .txt files

#1 Post by Costy » 25 Feb 2018 10:46

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to get lines between two strings and to put the results in a .txt files

#2 Post by penpen » 03 Mar 2018 08:11

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:

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
penpen

Costy
Posts: 4
Joined: 25 Feb 2018 10:43

Re: How to get lines between two strings and to put the results in a .txt files

#3 Post by Costy » 04 Mar 2018 02:17

Perfect!!!!!!!! It works. Thank you so much for reply. Best Regard ;)

Post Reply