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!
JREPL - search based on position?
Moderator: DosItHelp
Re: JREPL - search based on position?
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
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:
--OUTPUT--
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:
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:
--OUTPUT--
Dave Benham
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
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
Code: Select all
bawl,bag,block flog,lag,tag,sack,stack,mare,stare
Code: Select all
jrepl a o /p "((?:[^,]*,){2})([^,]*)" /pflag "" /prepl "$1+{$2}" /f test.txt
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
Code: Select all
bawl,bag,block flog,lag,tag,sock,stack,mare,store
Dave Benham
Re: JREPL - search based on position?
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:
Since my file names have the date, I want to keep this dynamic - anyway around this? Thanks again!
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 -
)
)
)
Re: JREPL - search based on position?
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
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
Re: JREPL - search based on position?
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?SIMMS7400 wrote: ↑04 Jul 2018 02:30HI 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:
Since my file names have the date, I want to keep this dynamic - anyway around this? Thanks again!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 - ) ) )
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.
Re: JREPL - search based on position?
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!
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!