JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Nuvolarix
Posts: 8
Joined: 11 Jan 2016 03:13

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#121 Post by Nuvolarix » 15 Jan 2016 10:19

Glad to be helpful, thank you for this great batch and for the tips :-)

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#122 Post by thefeduke » 15 Jan 2016 16:35

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!)
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:

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 - 
I have edited my previous post with updated code to incorporate more suitable test data at: http://www.dostips.com/forum/viewtopic.php?p=44764#p44764
John A.

Nuvolarix
Posts: 8
Joined: 11 Jan 2016 03:13

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#123 Post by Nuvolarix » 16 Jan 2016 08:56

thefeduke wrote:

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.
This sounds much more 'pro' 8)

... 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... :roll:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#124 Post by foxidrive » 16 Jan 2016 10:05

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#125 Post by foxidrive » 16 Jan 2016 10:25

Nuvolarix wrote:I'm wondering if a better code can be written... :roll:


findrepl may actually be a better tool for you, Nuvolarix.

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

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#126 Post by dbenham » 17 Jan 2016 09:27

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

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.

zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#127 Post by zimxavier » 17 Jan 2016 11:04

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 ?
Last edited by zimxavier on 18 Jan 2016 09:36, edited 1 time in total.

Nuvolarix
Posts: 8
Joined: 11 Jan 2016 03:13

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#128 Post by Nuvolarix » 18 Jan 2016 04:04

dbenham wrote:

Code: Select all

jrepl "^A$(?!\r?\nON$)" "$&\r\nON" /m /x /f before.txt /o after.txt
It works like a charme! So powerful... Thank you.
Last edited by Nuvolarix on 19 Jan 2016 01:06, edited 1 time in total.

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

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#129 Post by dbenham » 18 Jan 2016 10:04

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 /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 -


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 :lol:).

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

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

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#130 Post by dbenham » 18 Jan 2016 10:18

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.
No doubt findrepl is useful, but JREPL can also search within a found passage, though there is no option specifically for that feature.

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. :D


Dave Benham

zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#131 Post by zimxavier » 18 Jan 2016 17:42

Thank you Dave for your help.

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

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

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#132 Post by dbenham » 18 Jan 2016 22:37

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

Code: Select all

100aaaaa200aaa300aaaa400aaa


Dave Benham

zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#133 Post by zimxavier » 19 Jan 2016 03:46

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 be

Code: 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.

:idea: Could you test it ? It is exactly my example.
http://www.mediafire.com/download/faerb ... /batch.zip

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

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#134 Post by dbenham » 19 Jan 2016 07:18

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 :!: :shock:

The script works if you simply change for /r "\FOLDER" to for /r "FOLDER"


Dave Benham

zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

Re: JREPL.BAT - regex text processor - successor to REPL.BAT

#135 Post by zimxavier » 19 Jan 2016 09:36

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.

Post Reply