How to cut off some blocks off the entire file then save into separated file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
goodywp
Posts: 183
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#16 Post by goodywp » 24 Nov 2018 05:27

If I want to remove more lines as below code

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M72"
set "$fileOu=Load_NAR.M72"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2^>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1

			if defined $ if "!$:;---- Component: NAR KIA Tetra 0521=!" NEQ "!$!" set /A $filter = 1
			
			if defined $ if "!$:;---- Catalog: 8295000521_KIA.MXX=!" NEQ "!$!" set /A $filter = 1

			if defined $ if "!$:8295000521_KIA.P3A=!" NEQ "!$!" set /A $filter = 1

			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)

exit /b
like these 3 lines
if defined $ if "!$:;---- Component: NAR KIA Tetra 0521=!" NEQ "!$!" set /A $filter = 1

if defined $ if "!$:;---- Catalog: 8295000521_KIA.MXX=!" NEQ "!$!" set /A $filter = 1

if defined $ if "!$:8295000521_KIA.P3A=!" NEQ "!$!" set /A $filter = 1

it is version related, 0521,, how I can not hard code the above 3 lines, I tried * not working, any thought?
Thanks

goodywp
Posts: 183
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#17 Post by goodywp » 24 Nov 2018 07:47

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M72"
set "$fileOu=Load_NAR.M72"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2^>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1

			if defined $ if "!$:;---- Component: NAR KIA Tetra 0521=!" NEQ "!$!" set /A $filter = 1
			
			if defined $ if "!$:;---- Catalog: 8295000521_KIA.MXX=!" NEQ "!$!" set /A $filter = 1
			
			if defined $ if "!$:8295000521_KIA.P3A=!" NEQ "!$!" set /A $filter = 1

			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)

exit /b
I tried to remove more string as above code, however, these strings are version related...So looks like the code has hardcoded. Later on different versions of these string, the above code will not work... tried used *, not working...
Thanks

Squashman
Expert
Posts: 4013
Joined: 23 Dec 2011 13:59

Re: How to cut off some blocks off the entire file then save into separated file

#18 Post by Squashman » 24 Nov 2018 10:05

Starting to sound like scope creep.

goodywp
Posts: 183
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#19 Post by goodywp » 26 Nov 2018 08:56

Hi ED,

Since I had two questions related tho this post, which are as below:
1) The code in this post is working for cutting off the block before certain string in the middle of the text, then also try to remove certain strings in the remaining (keeping) to end.
If I want to delete some strings which are changeable, how I can do that, I tried using wildcard * in the string, not working, this one is asking some variety of the cut string ?
2) My question will be how to cut off the part starting from certain string in the middle, in other words, keep the beginning string, down to the middle part of string, then cut off
the rest of all to the end.

Hi Squashman,

Could you please kindly advise if I should open up a new post for the above corresponding questions...?

Thanks

Ed Dyreen
Expert
Posts: 1527
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to cut off some blocks off the entire file then save into separated file

#20 Post by Ed Dyreen » 26 Nov 2018 09:44

You could use findstr which does support regular expressions

Code: Select all

Echo.Hello There |findstr ".*Th?r?"
But you should usually try to avoid findstr because this slows down significantly.

With the faster set you can cut off the start of a variable

Code: Select all

set "$=Hello There"
set "$=%$:* =%"%=	strip off 'Hello '	=%
Or exact matches

Code: Select all

set "$=Hello There"
set "$=%$:Hello =%"%=	strip off 'Hello '	=%
Or positions

Code: Select all

set "$=Hello There"
set "$=%$:~6%"%=	strip off 'Hello '	=%
But the latter requires search and evaluation which also slows down significantly.

For more info type set /? and findstr /?

vbscript and jscript is much more suitable for such a job.

goodywp
Posts: 183
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#21 Post by goodywp » 26 Nov 2018 11:14

Ed Dyreen wrote:
26 Nov 2018 09:44
You could use findstr which does support regular expressions

Code: Select all

Echo.Hello There |findstr ".*Th?r?"
But you should usually try to avoid findstr because this slows down significantly.

With the faster set you can cut off the start of a variable

Code: Select all

set "$=Hello There"
set "$=%$:* =%"%=	strip off 'Hello '	=%
Or exact matches

Code: Select all

set "$=Hello There"
set "$=%$:Hello =%"%=	strip off 'Hello '	=%
Or positions

Code: Select all

set "$=Hello There"
set "$=%$:~6%"%=	strip off 'Hello '	=%
But the latter requires search and evaluation which also slows down significantly.

For more info type set /? and findstr /?

vbscript and jscript is much more suitable for such a job.
Thanks ED!!

My 2nd question is as below:
how to cut off the part starting from certain string in the middle, in other words, keep the beginning string, down to the string in middle part of file, then cut off the rest to the end.
like this case, I would like to cut off strings starting from

;---- NAR signed binaries
................
................
till
end

Keep the upper part of the string into a separated file (just the opposite of the code we have)

I once tried the function provided in this site to do the job. But ended up not succeed...

Thanks for your help...


goodywp
Posts: 183
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#23 Post by goodywp » 26 Nov 2018 13:56

Squashman wrote:
26 Nov 2018 13:41
Now cross posted on StackOverFlow.
Hi, Squashman,

Please advise on this...

When I continue to ask the same topic related question, you said "Starting to sound like scope creep."

In order to avoid this, I would ask the similar question on StackOverFlow… you again gave your comment...


Would you please help on this? I tried to use DOStip function also not working...

Code: Select all

@ECHO OFF

REM.-- Prepare the Command Processor

SETLOCAL ENABLEEXTENSIONS

SETLOCAL ENABLEDELAYEDEXPANSION

 

echo.Extracting hello world block into "Load_SDK.M83" file

call:extractFromFile ";---- Groupe signed binaries" ";---- NAR signed binaries">"Load_SDK.M83"

:extractFromFile - extract lines from a file between begin and end mark

:: - %~1: begin mark, use '...$' mark to allow variable substitution

:: - %~2: optional end mark, default is end of file

:: - %~3: optional source file, default is THIS file

SETLOCAL

set bmk=;---- Groupe signed binaries

set emk=;---- NAR signed binaries

set src=C:\Files\Load_all.M83

set /a b=-1

set /a e=-1

if "%src%"=="" set src=%~f0& ::- if no source file then assume THIS file

for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%bmk%" "%~f0""') do (

set b=%%a

set bmk=%%b

)

if /i %b%==-1 echo.ERROR: begin mark '%bmk%' not found in '%src%'&GOTO:EOF

if "%emk%"=="" (set /a e=2000000000) ELSE (

for /f "delims=:" %%a in ('"findstr /n /b /c:"%emk%" "%~f0""') do (

if /i %b% LSS %%a if /i !e!==-1 set e=%%a& rem -- find only the first one after b

)

)

if /i %e%==-1 echo.ERROR: end mark '%emk%' missing in '%src%'&GOTO:EOF

if /i %b% GEQ %e% echo.ERROR: end mark '%emk%' detected before begin mark '%bmk%' in '%src%'&GOTO:EOF

for /f "delims=: tokens=1,*" %%a in ('"findstr /v /n /b /c:"#$*ReturnAll*$#" "%src%""') do (

if /i %b% LSS %%a if /i %%a LSS %e% (

if "%bmk:~-1%"=="$" (

rem --sustitution variables within %%b

call echo.%%b

) ELSE (

rem --no variable substitution

echo.%%b

)

)

)

GOTO:EOF

What can I do...

goodywp
Posts: 183
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#24 Post by goodywp » 29 Nov 2018 04:55

For changeable strings to remove from ED's original scripts, I just modified as below then tested and works fine for me.
Just cut off the changeable part of the strings then set as new variable, which will be used in ED's original code.
Thanks ED again.

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M72"
set "$fileOu=Load_NAR.M72"

set KIA1=;---- Component: NAR KIA Tetra 0521
set KIA1_var=%KIA1:~17,7%

set KIA2=;---- Catalog: 8295000521_KIA.MXX
set KIA2_var=%KIA2:~0,21%

set KIA3=8295000521_KIA.P3A
set KIA3_var=%KIA3:~0,6%


> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2^>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1

			if defined $ if "!$:%KIA1_var%=!" NEQ "!$!" set /A $filter = 1
			
			if defined $ if "!$:%KIA2_var%=!" NEQ "!$!" set /A $filter = 1

			if defined $ if "!$:%KIA3_var%=!" NEQ "!$!" set /A $filter = 1

			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)

exit /b

Post Reply