JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets
Moderator: DosItHelp
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
Glad to be helpful, thank you for this great batch and for the tips
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
You are most welcome, Nuv, but your revelation about the data file spurred my stubbornness to learn a bit more about this wondrous utility and I came up with:Nuvolarix wrote:Now I just miss the code to replenish it, or in other words, add the <Autorun> line where is not present. Unfortunately I cannot use the code suggested by thefeduke (btw, thank you too!)
Code: Select all
Call jrepl "(^\s*<file name=\qApp On\q>\s*<icon>icon.ico</icon>\s*<path>App.exe</path>[ \t]*\r?\n)[[ \t]*\r?\n?" "$1 <Autorun>2</Autorun>\r\n " /m /x /f "%tmpd%\%~n0_ToInsert.txt" /o -
John A.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
This sounds much more 'pro'thefeduke wrote:John A.Code: Select all
Call jrepl "(^\s*<file name=\qApp On\q>\s*<icon>icon.ico</icon>\s*<path>App.exe</path>[ \t]*\r?\n)[[ \t]*\r?\n?" "$1 <Autorun>2</Autorun>\r\n " /m /x /f "%tmpd%\%~n0_ToInsert.txt" /o -
... but I still need something 'more': I don't want the Autorun line to be added if it's already there, in other words, I don't want to add a second Autorun line, by running the code twice, the second run should change nothing!
Something that I can roughly achieve with this, using a typical ending line 'MRU':
Code: Select all
jrepl "(^\s*<file name=\qApp Off\q>\s*<icon>icon.ico</icon>\s*<path>App.exe</path>\s*<MRU>1</MRU>[ \t]*\r?\n)" "\r\n <file name=\qApp Off\q>\r\n <icon>icon.ico</icon>\r\n <path>App.exe</path>\r\n <Autorun>2</Autorun>\r\n <MRU>1</MRU>\r\n" /m /x /f test.txt /o -
where test.txt is the following:
Code: Select all
<file name="App X">
<icon>iconX.ico</icon>
<path>AppX.exe</path>
<Autorun>2</Autorun>
<MRU>1</MRU>
<file name="App Off">
<icon>icon.ico</icon>
<path>App.exe</path>
<Autorun>2</Autorun>
<MRU>1</MRU>
<file name="App Y">
<icon>iconY.ico</icon>
<path>AppY.exe</path>
<MRU>1</MRU>
<file name="App Z">
<icon>iconZ.ico</icon>
<path>AppZ.exe</path>
<Autorun>3</Autorun>
<MRU>1</MRU>
I'm wondering if a better code can be written...
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
thefeduke wrote:your revelation about the data file spurred my stubbornness to learn a bit more about this wondrous utility
It is a nice bit of kit John, ain't it!
Aacini has written a tool that also has nice features - findrepl.bat
viewtopic.php?f=3&t=4697
findrepl hasn't taken off as well as repl and the newer jrepl - but it has extra features that are useful, such as searching text for a passage, and then searching within that passage again. It can modify text as Jrepl does and they are both very powerful tools that use Jscript for speed and reliability.
If you like tinkering then it's worth having a look at.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
Nuvolarix wrote:I'm wondering if a better code can be written...
findrepl may actually be a better tool for you, Nuvolarix.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
The simplest way to conditionally add a line of text if and only if it does not yet exist is to use the standard regex negative look-ahead assertion (Note that JScript regular expressions do not support look-behind assertions)
Rather than work out the complicated regex needed for your case, I'll just provide a much simplified example.
Suppose I want Every line consisting of "A" to be followed by a single "ON" line. I only want to add the "ON" where it is missing, and leave the other instances alone. I search for the "A" line and then use the negative look-ahead to check if the "ON" line follows. It only matches when "ON" does not exist. My replacement is simply the full matching string "$&" with the "ON" line concatenated.
EDIT - modified search to make sure "A" matches the entire line
before.txt
after.txt
Dave Benham
Rather than work out the complicated regex needed for your case, I'll just provide a much simplified example.
Suppose I want Every line consisting of "A" to be followed by a single "ON" line. I only want to add the "ON" where it is missing, and leave the other instances alone. I search for the "A" line and then use the negative look-ahead to check if the "ON" line follows. It only matches when "ON" does not exist. My replacement is simply the full matching string "$&" with the "ON" line concatenated.
EDIT - modified search to make sure "A" matches the entire line
Code: Select all
jrepl "^A$(?!\r?\nON$)" "$&\r\nON" /m /x /f before.txt /o after.txt
before.txt
Code: Select all
A
B
AXE
FA
A
ON
C
A
ONLY
D
A
after.txt
Code: Select all
A
ON
B
AXE
FA
A
ON
C
A
ON
ONLY
D
A
ON
Dave Benham
Last edited by dbenham on 18 Jan 2016 08:49, edited 1 time in total.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
Hello,
Firstly,
My batch call jrepl.bat "_desc(.*)" "" /f SOURCE.txt /o - works as expected, but i wish now add a case-insensitive (for including "_desc" and "_DESC") :
call jrepl.bat "_desc(.*)" "0" /f /i SOURCE.txt /o -
Now, i have an error message and nothing happens :
Too many arguments
Secondly,
i would need to create a batch :
I have two files :
File1.txt
string1
string2
string3
etc
File2.txt
string1b
string2b
string3b
etc
I would like to replace in all .txt files in all subdirectories in \FOLDER any string n from File1.txt with string n from File2.txt (i.e. string1 with string1b, string2 with string2b, string3 with string3b, etc).
Could you help me ?
Firstly,
My batch call jrepl.bat "_desc(.*)" "" /f SOURCE.txt /o - works as expected, but i wish now add a case-insensitive (for including "_desc" and "_DESC") :
call jrepl.bat "_desc(.*)" "0" /f /i SOURCE.txt /o -
Now, i have an error message and nothing happens :
Too many arguments
Secondly,
i would need to create a batch :
I have two files :
File1.txt
string1
string2
string3
etc
File2.txt
string1b
string2b
string3b
etc
I would like to replace in all .txt files in all subdirectories in \FOLDER any string n from File1.txt with string n from File2.txt (i.e. string1 with string1b, string2 with string2b, string3 with string3b, etc).
Could you help me ?
Last edited by zimxavier on 18 Jan 2016 09:36, edited 1 time in total.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
It works like a charme! So powerful... Thank you.dbenham wrote:Code: Select all
jrepl "^A$(?!\r?\nON$)" "$&\r\nON" /m /x /f before.txt /o after.txt
Last edited by Nuvolarix on 19 Jan 2016 01:06, edited 1 time in total.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
The /F option takes the next argument as the path/file name. But you put the /I after the /F, so /F things /I is the file name, and SOURCE.txt is an invalid option, hence the error message.zimxavier wrote:My batch call jrepl.bat "_desc(.*)" "" /f SOURCE.txt /o - works as expected, but i wish now add a case-insensitive (for including "_desc" and "_DESC") :
call jrepl.bat "_desc(.*)" "0" /f /i SOURCE.txt /o -
Now, i have an error message and nothing happens :
Too many arguments
The solution is obvious:
Code: Select all
call jrepl.bat "_desc(.*)" "0" /i /f SOURCE.txt /o -
zimxavier wrote:I have two files :
File1.txt
string1
string2
string3
etc
File2.txt
string1b
string2b
string3b
etc
I would like to replace in all .txt files in all subdirectories in \FOLDER any string n from File1.txt with string n from File2.txt (i.e. string1 with string1b, string2 with string2b, string3 with string3b, etc).
You will want the /T "delimiter" option for this, which was introduced in JREPL version 2, (with numerous subsequent bug fixes ).
You must load the lines from File1.txt into a single environment variable, and likewise with File2.txt. The lines must be delimited by a character of your choice that does not appear in any of the search or replacement strings. Remember that the maximum variable length is a bit less than 8191 characters, so there is a limit to how many strings you can handle.
Once loaded, you can use the /V option to indicate the search and replace come from environment variables.
Based on how you described your problem, you will want to treat the search strings as literals with the /L option.
JREPL can only process one file, so you will need your own FOR /R loop. You can only use FOR /R because you are not creating any new files in the directory structure, so there is no risk of processing a file twice. If you wrote new files instead, then you would need to switch to FOR /F with the DIR /S /B command.
Reading files in batch is a pain if you want the code to work regardless what the content is. For example, exclamation points cause problems with FOR /F if delayed expansion is enabled. To simplify things, I'm going to assume that your File1.txt and File2.txt do not contain any empty lines, and all lines are <=1021 bytes long. This enables me to use SET /P to read the lines, and the absence of line content will indicate end of file.
I will assume none of your search strings contain the pipe symbol, and I'll use that character for the /T delimiter.
Code: Select all
@echo off
setlocal enableDelayedExpansion
set "find="
call :loadFile find <"File1.txt"
set "repl="
call :loadFile repl <"File2.txt"
for /r "\FOLDER" %%F in (*.txt) do call jrepl find repl /v /l /t "|" /f "%%F" /o -
exit /b
:loadFile rtnVar
set "ln="
set /p "ln="
if defined ln (
set "%1=!%1!|!ln!"
goto :loadFile
)
set "%1=!%1:~1!
exit /b
Dave Benham
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
No doubt findrepl is useful, but JREPL can also search within a found passage, though there is no option specifically for that feature.foxidrive wrote:findrepl hasn't taken off as well as repl and the newer jrepl - but it has extra features that are useful, such as searching text for a passage, and then searching within that passage again.
The /J option (or /JMATCH option) treats the replacement string as JScript, which can be arbitrarily complex. The $0 variable contains the matched passage, and there is nothing stopping you from using $0.search(...), etc. within your replacement string.
Dave Benham
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
Thank you Dave for your help.
D'oh ! Thank you.
Nothing happens when i execute this batch. I added a "pause" at the end, but the window disappears quickly. Sorry, i created some very basic batchs and it is hard for me to understand it. Maybe i missed something. In concrete terms where i have to add a delimiter ?
I have :
\FOLDER\a.txt
Batch.bat (your code without any change)
File1.txt
File2.txt
JREPL.bat
File1.txt
File2.txt
\FOLDER\a.txt
My wish :
dbenham wrote:The /F option takes the next argument as the path/file name. But you put the /I after the /F, so /F things /I is the file name, and SOURCE.txt is an invalid option, hence the error message.
The solution is obvious:Code: Select all
call jrepl.bat "_desc(.*)" "0" /i /f SOURCE.txt /o -
D'oh ! Thank you.
dbenham wrote:@echo off
setlocal enableDelayedExpansion
set "find="
call :loadFile find <"File1.txt"
set "repl="
call :loadFile repl <"File2.txt"
for /r "\FOLDER" %%F in (*.txt) do call jrepl find repl /v /l /t "|" /f "%%F" /o -
exit /b
:loadFile rtnVar
set "ln="
set /p "ln="
if defined ln (
set "%1=!%1!|!ln!"
goto :loadFile
)
set "%1=!%1:~1!
exit /b
[/code]
Dave Benham
Nothing happens when i execute this batch. I added a "pause" at the end, but the window disappears quickly. Sorry, i created some very basic batchs and it is hard for me to understand it. Maybe i missed something. In concrete terms where i have to add a delimiter ?
I have :
\FOLDER\a.txt
Batch.bat (your code without any change)
File1.txt
File2.txt
JREPL.bat
File1.txt
1
2
3
4
File2.txt
10
20
30
40
\FOLDER\a.txt
1aaaaa2aaa3aaaa4aaa
My wish :
10aaaaa20aaa30aaaa40aaa
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
Did you look at the content of FOLDER\a.txt after you ran the script? It worked perfectly for me.
Bear in mind that it will only give the correct result the first time. If you run it a second time, then the result will be
Dave Benham
Bear in mind that it will only give the correct result the first time. If you run it a second time, then the result will be
Code: Select all
100aaaaa200aaa300aaaa400aaa
Dave Benham
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
dbenham wrote:Did you look at the content of FOLDER\a.txt after you ran the script? It worked perfectly for me.
Bear in mind that it will only give the correct result the first time. If you run it a second time, then the result will beCode: Select all
100aaaaa200aaa300aaaa400aaa
After i ran Batch.bat the content of a.txt is exactly the same as before. The date hasn't changed. I tested a second time right now.
Could you test it ? It is exactly my example.
http://www.mediafire.com/download/faerb ... /batch.zip
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
You must have "FOLDER" in the same directory as your "batch.bat". But in your question you used "\FOLDER", which is off the root directory.
I was surprised that FOR /R does not give an error if the specified directory does not exists
The script works if you simply change for /r "\FOLDER" to for /r "FOLDER"
Dave Benham
I was surprised that FOR /R does not give an error if the specified directory does not exists
The script works if you simply change for /r "\FOLDER" to for /r "FOLDER"
Dave Benham
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
It works, thank you !
Sorry, i've gotten used to adding a "\" not before but after a folder (otherwise it asks me if it is a file or a folder). I don't know why this time i put it before...
Maybe FOR /R gives me an error, but it is too fast, i can't read anything.
Sorry, i've gotten used to adding a "\" not before but after a folder (otherwise it asks me if it is a file or a folder). I don't know why this time i put it before...
Maybe FOR /R gives me an error, but it is too fast, i can't read anything.