Generic Find & Replace vs JREPL?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Generic Find & Replace vs JREPL?

#1 Post by SIMMS7400 » 13 Dec 2017 03:00

Hi Dave & Team -

I have a rather large data file I run a find and replace over using the following syntax :

Code: Select all

    
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
	
SET FILE_SEARCH=%GJA_S3%
SET FILE_REPLACE=%HFM_VAR%
SET FILE_PATH=%GJA_PROCPATH%

SET "FN=HFMFile_Target_%GJA_S3U%.txt"
SET "FILE=%FILE_PATH%%FN%"
	
FOR /f "delims=" %%L IN ('TYPE "%FILE%" ^& BREAK ^> "%FILE%" ') DO (
	SET "LINE=%%L"
        SETLOCAL ENABLEDELAYEDEXPANSION
        SET "LINE=!LINE:%FILE_SEARCH%=%FILE_REPLACE%!"
        >>"%FILE%" ECHO !LINE!
        ENDLOCAL
)
Would JREPL be more efficient? Particularly with the new jmatch functionality?

Thank you!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generic Find & Replace vs JREPL?

#2 Post by aGerman » 13 Dec 2017 05:39

You should try it to answer this question but there is a good chance JREPL is more efficient because
1) FOR /F buffers the file content before it even begins to iterate. That's the reason why you are able to write back to the same file without loss of data.
2) You toggle delayed expansion for every line which is slow.
3) Using >> you open the file stream repeatedly for every line which is slow.

Steffen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Generic Find & Replace vs JREPL?

#3 Post by dbenham » 13 Dec 2017 05:41

Why wouldn't you simply try for yourself :?: :?
It wouldn't be much harder than asking a question.

But, yes, JREPL will be much faster, unless your file is small. And JREPL hasn't any restrictions on character strings, unlike batch variable find/replace, which cannot do a search that includes = or starts with *. Plus you can do significantly more complex searches with JREPL due to the regular expression capabilities and many options.

There are techniques available to make your pure batch script significantly faster. But JREPL will still outperform an optimized pure batch script unless the file is small.

You are doing a literal search, so you want the /L option.

Your search and replace strings are in variables, so you use the /V option.

You should use the /XSEQ option in case either string includes non-ASCII (byte values >127).

Use the /F option to specify your input file, and /O - to overwrite the original file with the result.

Code: Select all

call jrepl GJA_S3 HFM_VAR /v /xseq /l /f "%GJA_PROCPATH%HFMFile_Target_%GJA_S3U%" /o -
/JMATCH is used for extracting matching strings (with possible manipulation) throwing away the parts of the file that do not match. So it is not relevant in your situation. And if you ever were to use it, you should use /JMATCHQ instead for far superior speed.


Dave Benham

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Generic Find & Replace vs JREPL?

#4 Post by SIMMS7400 » 13 Dec 2017 09:28

Hi Dave -

Thanks for the followup - much appreciated!

Just tried to run a test and not getting expected results - amd I messing this up?

Code: Select all

@ECHO OFF
pushd C:\Hyperion_Batch\Scripts\Utilities\JREPL

SET "SEARCH=START"
SET "REPLACE=FINISH"

call JREPL.bat %SEARCH% %REPLACE% /v /xseq /l /f "TEST.txt" /o -
pause
popd
And my file only has START in it, but it's not changing?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generic Find & Replace vs JREPL?

#5 Post by aGerman » 13 Dec 2017 09:32

You changed the working directory using PUSHD. Is "TEST.txt" also in "C:\Hyperion_Batch\Scripts\Utilities\JREPL"?

Steffen

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Generic Find & Replace vs JREPL?

#6 Post by SIMMS7400 » 13 Dec 2017 09:34

Yes it is - also the date modified stamp is getting updated, just nothing happening to the search string.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generic Find & Replace vs JREPL?

#7 Post by aGerman » 13 Dec 2017 09:48

As Dave said the /v option requires the variable names instead of their values.

Code: Select all

call JREPL.bat SEARCH REPLACE /v /xseq /l /f "TEST.txt" /o -
Steffen

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Generic Find & Replace vs JREPL?

#8 Post by SIMMS7400 » 13 Dec 2017 10:18

I need to get better at following directions LOL

Thank you, Steffen - working like a charm now!

Post Reply