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