JREPL - search based on position?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

JREPL - search based on position?

#1 Post by SIMMS7400 » 02 Jul 2018 16:33

Hi There -

I use JREPL to perform various find & replaces. However, I only need it to perform the find & replace on the 3rd column. My delimiter is comma.

Thank you!

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

Re: JREPL - search based on position?

#2 Post by dbenham » 03 Jul 2018 06:36

If javascript regular expressions supported look behind assertions then it would be easy. But alas, no.

That is one of the main reasons I added the /PREPL option in version 7.9, that augments the /P and /PFLAG options introduced in version 6.2.

The /P option uses a regex to identify which portion of a line will participate in the find/replace.
The /PFLAG options specifies what flags to use for the /P search - Defaults to global case sensitive search.
The /PREPL option identifies which captured groups within /P are left alone, and which are actually replaced.

Use JREPL /?/P etc. to get detailed help about each of the options.

So lets say you have the following one line text file:

test.txt

Code: Select all

bawl,bag,black flag,lag,tag,sack,stack,mare,stare
And you want to substitute "o" for every "a" within the 3rd column only.

This first solution uses the "^" anchor to make sure /P only searches through the 3rd column:

Code: Select all

jrepl a o /p "^((?:[^,]*,){2})([^,]*)" /prepl "$1+{$2}" /f test.txt
--OUTPUT--

Code: Select all

bawl,bag,block flog,lag,tag,sack,stack,mare,stare
Alternatively you can drop the "^" and use the /PFLAG option to remove the default g flag so that only the first matching /P is used:

Code: Select all

jrepl a o /p "((?:[^,]*,){2})([^,]*)" /pflag "" /prepl "$1+{$2}" /f test.txt
Output is the same as before.


Now suppose you want to substitute "o" for "a" in every third column (columns 3,6,9...).

The /P option has to be modified because each 3rd column might end with comma or end-of-line:

Code: Select all

jrepl a o /p "((?:[^,]*,){2})([^,]*)(,|$)" /prepl "$1+{$2}+$3" /f test.txt
--OUTPUT--

Code: Select all

bawl,bag,block flog,lag,tag,sock,stack,mare,store

Dave Benham

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

Re: JREPL - search based on position?

#3 Post by SIMMS7400 » 04 Jul 2018 02:30

HI Dave -

Thank you so much! Works like an absolute charm!!!

Also, how come JREPL won't work if you use a variable for the file name? For instance, this doesn't work:

Code: Select all

PUSHD "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%YYYY_MMDD%"
	FOR %%A IN ("*.csv") DO (
		SET "NAME=%%A"
		FOR %%a IN ( %LIST% ) DO (
			SET "SKIP="
			ECHO %%A | FINDSTR /C:"_%%~a_" >nul 2>&1 && SET "SKIP=T"
			IF DEFINED SKIP (
				CALL "%UTILPATH%JREPL\JREPL.bat" 3 1 /p "^((?:[^,]*,){2})([^,]*)" /prepl "$1+{$2}" /f "!NAME!" /o - 
			)
		)
	)
	
Since my file names have the date, I want to keep this dynamic - anyway around this? Thanks again!

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

Re: JREPL - search based on position?

#4 Post by dbenham » 04 Jul 2018 06:22

There is nothing special about JREPL about using a variable in a file name. If you have a problem there, then the problem is in your logic, totally separate from JREPL.

Do you have delayed expansion enabled? - SETLOCAL ENABLEDELAYEDEXPANSION

But I see no reason to define the variable NAME, you can use %%A directly. Then you don't have to worry about delayed expansion.


Dave Benham

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

Re: JREPL - search based on position?

#5 Post by Squashman » 04 Jul 2018 10:19

SIMMS7400 wrote:
04 Jul 2018 02:30
HI Dave -

Thank you so much! Works like an absolute charm!!!

Also, how come JREPL won't work if you use a variable for the file name? For instance, this doesn't work:

Code: Select all

PUSHD "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%YYYY_MMDD%"
	FOR %%A IN ("*.csv") DO (
		SET "NAME=%%A"
		FOR %%a IN ( %LIST% ) DO (
			SET "SKIP="
			ECHO %%A | FINDSTR /C:"_%%~a_" >nul 2>&1 && SET "SKIP=T"
			IF DEFINED SKIP (
				CALL "%UTILPATH%JREPL\JREPL.bat" 3 1 /p "^((?:[^,]*,){2})([^,]*)" /prepl "$1+{$2}" /f "!NAME!" /o - 
			)
		)
	)
	
Since my file names have the date, I want to keep this dynamic - anyway around this? Thanks again!
Your logic seems backwards to me. If the variable skip is defined or true, that makes me think you would want to skip processing that file. You are defining the variable skip and then if it is defined then running JREPL. Why wouldn't you just CALL JREPL with the conditional execution instead defining the variable?

Also, it seems like this code could be shortened even further if you flipped the order of the FOR commands or used the list as a way to make a whole variable with a wildcard string to use with the initial FOR command to get all the csv files.

It seems like an extreme inefficiency to check the file name for every possible outcome within your list variable. You could even take your list and make it into a file that FINDSTR could use for the search arguments. I know I have shown you that in the past.

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

Re: JREPL - search based on position?

#6 Post by SIMMS7400 » 07 Jul 2018 19:17

Dave -

Thank you so much! Yes, it seemed to be an issue with my syntax. I'm using %%A with success!

Squash -

Thank you for the suggestions. I too agree it's a bit backwards. I'm amidst making changes to the process and this is just a interim version. I will be cleaning it up as you suggested. As far as the FINDSTR command, the strings it's search its just (3) and they are actually parts of file names that I use in other parts of this process as well. While a file could work, I just wanted one point of maintenance so I included them in a variable.

Thanks again gents!

Post Reply