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
zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

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

#136 Post by zimxavier » 22 Jan 2016 08:42

Hi

I posted a new case here. Can i use repl.bat for improving my batch ? Or maybe you know where i can find more informations about FOR ?

Thank you

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#137 Post by ShadowThief » 22 Jan 2016 15:56

zimxavier wrote:Or maybe you know where i can find more informations about FOR?

You can find information about every command by opening a command prompt and typing the command followed by a /? In your case,

Code: Select all

for /?


You can also read through http://ss64.com/nt/, which is the same information, but with less typing on your part.

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

#138 Post by dbenham » 22 Jan 2016 17:12

ShadowThief wrote:You can also read through http://ss64.com/nt/, which is the same information, but with less typing on your part.

SS64 has much more than the standard HELP info - it includes lots of under-documented and/or undocumented features, as well as practical examples. Much of the information has been collected from a combination of the SS64 CMD forum, DOSTIPS forum, and StackOverflow, among others.


Dave Benham

NewYears1978
Posts: 8
Joined: 29 Nov 2015 21:42

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

#139 Post by NewYears1978 » 25 Jan 2016 00:30

I was directed here when I asked a question about something but they weren't sure if this did what I need or not.

What I have are two files, they are settings files for a program I use. I edit the settings on my end and I need end users to be able to put my files with theirs and run the bat to replace text in theirs with text in mine.

(Okay that sounded confusing)

Short version: Can it go to a certain point in a file and then replace ALL text after that with ALL text in another file after a string?

In other words:

File 1:

Some Text
Some Text
[A word in brackets]
File1's Text

File 2:
Some Text
Some Text
[A word in brackets]
File2's Text


I want to take all the text after the word in brackets from File2 and input it in File 1 leaving only the lines before the bracketed word.
Hope this makes sense.

This may not be possible with this tool but I figured I would ask before I tried to figure it out.

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

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

#140 Post by foxidrive » 25 Jan 2016 05:55

findrepl.bat is useful there, if the start and ending text is unique.

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

#141 Post by dbenham » 25 Jan 2016 07:29

@NewYears1978 - Sure, quite easily:

Code: Select all

@echo off
call jrepl "^\[AWordInBrackets]$" "quit=1;$0" /j /f file1.txt /o -

call jrepl "^\[AWordInBrackets]$ ^.*$"^
           "go+=1;go<2?false:$0; go?$0:false"^
           /t " " /jmatch /jbeg "go=0" /f file2.txt >> file1.txt

The first JREPL is really simple - it removes all lines after the word in brackets. It does this by searching for the bracketed word, and when found, sets quit to 1 (true), and substitutes with itself. The output is directed to overwrite the original via /o -.

The second JREPL is a bit more complicated - it relies on the /T option that behaves similar to the unix tr command. Variable go is initialized to 0. Only matched text is printed. The search looks for either the bracketed word, or any other line. If the bracketed word is found, then go is incremented, and the text is discarded if go<2, else it is printed (just in case the bracketed word appears a 2nd time in file2). Any other line is only printed if go is non-zero. The output is appended to file1.txt.


Dave Benham

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

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

#142 Post by thefeduke » 26 Jan 2016 00:22

dbenham wrote:The first JREPL is really simple - it removes all lines after the word in brackets. It does this by searching for the bracketed word, and when found, sets quit to 1 (true), and substitutes with itself.
Thank you, Dave, for these last two examples. These code models are teaching me enough to plagiarize myself into a small problem with this exercise:

Code: Select all

    @Echo Off &SetLOCAL EnableDelayedExpansion &Rem.Keep environment uncluttered.
Rem.DOS Testing Environment '0' easy to find temporary folder
    Set "tmpd=%TEMP%\'0'"
    If Not Exist "%tmpd%" MkDir "%tmpd%"
::
    Set "Ref=ccc"
(
    echo(xxx
    echo(aaa
    echo(bbb
    echo(%Ref%
    echo(xxx
    echo(%Ref%
    echo(ddd
    echo(xxx
    echo(xxx
    echo(%Ref%
    echo(xxx
)>%tmpd%\%~n0_in.txt

    @echo.
    @echo.input records
    type "%tmpd%\%~n0_in.txt"
    @echo..

    call jrepl "^%Ref%$" "quit=1;$0" /j ^
    /f "%tmpd%\%~n0_in.txt" /o "%tmpd%\%~n0_out1.txt"
    @echo.
    @echo.copy until and keep record '%Ref%'
    type "%tmpd%\%~n0_out1.txt"
    @echo..

    call jrepl "^%Ref%$" "quit=1;$" /j ^
    /f "%tmpd%\%~n0_in.txt" /o "%tmpd%\%~n0_out2.txt"
    @echo.
    @echo.copy until and omit record '%Ref%'
    type "%tmpd%\%~n0_out2.txt"
    @echo..

    call jrepl "^%Ref%$ ^.*$"^
               "go+=1;go<3?false:$0; go?$0:false"^
               /t " " /jmatch /jbeg "go=0"^
               /f "%tmpd%\%~n0_in.txt" > "%tmpd%\%~n0_out3.txt"
    @echo.
    @echo.copy from and omit first two '%Ref%' referenced records
    type "%tmpd%\%~n0_out3.txt"
    @echo..

    call jrepl "^%Ref%$ ^.*$"^
               "go+=1;go<-2?false:$0;$0; go?$0:false"^
               /t " " /jmatch /jbeg "go=0"^
               /f "%tmpd%\%~n0_in.txt" > "%tmpd%\%~n0_out3.txt"
    @echo.
    @echo.copy from and keep referenced record '%Ref%'
    type "%tmpd%\%~n0_out3.txt"

    @echo..
    GoTo :EoF
[Edit: added missing variable definition for '%tmpd%']
The second JREPL produces exactly what I wanted but with the following error message:

Code: Select all

JScript runtime error in Replace code: '$' is undefined

copy until and omit record 'ccc'
xxx
aaa
bbb
.

I can mask the error with '2>nul', but I am being told that I used '$' Illegally. Would you please show the proper 'replace' string to produce the same output?

I also have a very rudimentary question. While I experiment, my errors occasionally thrust me into input mode for stdin. I do not know the inKEYtation to say that the input is complete, so I just Ctrl-Break out of it. Anyone?

John A
Last edited by thefeduke on 26 Jan 2016 15:58, edited 1 time in total.

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

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

#143 Post by foxidrive » 26 Jan 2016 09:06

$ is a regular expression for the end of line - and you have an extra one there which would need to be escaped as in \$ if it is intended as a literal $ sign.

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

#144 Post by dbenham » 26 Jan 2016 10:18

To close input, press <Ctrl-Z> and press <Enter>.

I'm not sure what is causing the 2nd JREPL to wait for input, but I know your logic is wrong. You want to match the %Ref% line and discard it, which means you would need to use /JMATCH and set the replacement value to false. It also means you must use the /T option and preserve all the earlier lines.

Code: Select all

call jrepl "^%Ref%$ ^.*" "quit=1;false $0" /jmatch /t " "*
          /f "%tmpd%\%~n0_in.txt" /o "%tmpd%\%~n0_out2.txt"


But I realized there is a relatively simple solution to NewYears1978's original problem that avoids any JScript. It relies on the /M multi-line option.

Code: Select all

@echo off
call jrepl "([\s\S]*?^\[AWordInBrackets\]\r?\n)[\s\S]*" $1 /m /f file1.txt /o -
call jrepl "[\s\S]*?^\[AWordInBrackets\]\r?\n([\s\S]*)" $1 /m /f file2.txt >> file1.txt


Using the above principle, thefeduke's code can become (note - I defined tmpd to be the current directory):

Code: Select all

@Echo Off
SetLOCAL EnableDelayedExpansion
set "tmpd=."
Set "Ref=ccc"
(
    echo(xxx
    echo(aaa
    echo(bbb
    echo(%Ref%
    echo(xxx
    echo(%Ref%
    echo(ddd
    echo(xxx
    echo(xxx
    echo(%Ref%
    echo(xxx
)>%tmpd%\%~n0_in.txt

echo.
echo.input records
type "%tmpd%\%~n0_in.txt"
echo..

call jrepl "([\s\S]*?^%Ref%\r?\n)[\s\S]*" $1^
           /m /f "%tmpd%\%~n0_in.txt" /o "%tmpd%\%~n0_out1.txt"
echo.
echo.copy until and keep record '%Ref%'
type "%tmpd%\%~n0_out1.txt"
echo..

call jrepl "([\s\S]*?\n)%Ref%\r?\n[\s\S]*" $1^
           /m /f "%tmpd%\%~n0_in.txt" /o "%tmpd%\%~n0_out2.txt"
echo.
echo.copy until and omit record '%Ref%'
type "%tmpd%\%~n0_out2.txt"
echo..

call jrepl "[\s\S]*?^%Ref%\r?\n([\s\S]*?)^%Ref%\r?\n([\s\S]*)" $1$2^
           /m /f "%tmpd%\%~n0_in.txt" /o "%tmpd%\%~n0_out3.txt"
echo.
echo.copy from and omit first two '%Ref%' referenced records
type "%tmpd%\%~n0_out3.txt"
echo..

call jrepl "[\s\S]*?(^%Ref%$[\s\S]*)" $1^
           /m /f "%tmpd%\%~n0_in.txt" /o "%tmpd%\%~n0_out4.txt"
echo.
echo.copy from and keep referenced record '%Ref%'
type "%tmpd%\%~n0_out4.txt"

echo..
GoTo :EoF


Dave Benham

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

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

#145 Post by thefeduke » 26 Jan 2016 16:20

dbenham wrote:To close input, press <Ctrl-Z> and press <Enter>.

I'm not sure what is causing the 2nd JREPL to wait for input, but I know your logic is wrong.
Thank you, Dave. The waiting for input neophyte question was not directly related, but my occasional coding errors have nullified the input file but allowed the JREPL to run.
Incidentally, I edited my script above, because I had neglected to define '%tmpd%', but you already took that into account with your rework. Thanks again, for that 'above and beyond' re-coding that gives more exploration room in my 'tutor by example' education.

John A.

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

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

#146 Post by zimxavier » 28 Jan 2016 16:28

Hello !

Sorry, i post some questions again because i didn't find exactly what i needed. :( I think these cases are relatively basic for an experienced user.

**First situation**
My current batch works:

Code: Select all

call jrepl.bat ":(.*)" "" /f FILE.txt /o -


For each line, it removes all characters from the first colon to the end of line in FILE.txt.
I now need to make the same thing but retaining the first colon.

FILE.txt without changes:
dg_islam.014.t: "Missionnaires soufis"
dg_islam.015.t: "Des élites musulmanes apparaissent en $COUNTRY$"
dg_islam.011.a: "Nos rangs se renforcent."
dg_islam.010.a: "Envoyons-les à la frontière."

FILE.txt after my original batch:
dg_islam.014.t
dg_islam.015.t
dg_islam.011.a
dg_islam.010.a

FILE.txt after my possible future batch:
dg_islam.014.t:
dg_islam.015.t:
dg_islam.011.a:
dg_islam.010.a:



**Second situation**
I have csv files in a folder. A semi-colon is the separator between column. I would like to remove for each line all characters after the third semi-colon (or after the third column if i open it with Excel)
A line from one of my csv files :
alert_can_reform_religion_name;§YReligion can be Reformed§!;§YReligion peut être réformée§!;§YReligion kann reformiert werden§!;;§YLa Religión puede ser Reformada§!;;;;;;;;;x

What i wish :
alert_can_reform_religion_name;§YReligion can be Reformed§!;§YReligion peut être réformée§!;


**Third situation**
A batch from Dave removes from FILE.txt all spaces and tabulations at the beginning and at the end of each line, as well as all blank lines.

batch.bat
call REMOVE_BLANK "FILE.txt"

remove_blank.bat
@jrepl "^[ \t]*(.*?)[ \t]*$" "$1?$1:false" /jmatch /f %1 /o -


- It works fine, but spaces at the beginning of line are important for me. How could we write the same thing but retaining spaces (or tabulations) at the beginning of line ?
- Can i put that line directly in my batch.bat ?

Thanks in advance.

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

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

#147 Post by foxidrive » 28 Jan 2016 23:15

The first item:

Code: Select all

call jrepl.bat "(:)(.*)" "$1" /f FILE.txt /o -


Second item:

Code: Select all

call jrepl "^(.*?;.*?;.*?;).*" "$1" /f FILE.txt /o -


I don't have experience with the third format using jmatch.

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

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

#148 Post by zimxavier » 29 Jan 2016 08:48

Thank you foxidrive. Your 2 batchs work.
In my second situation, i have many csv files and their names regularly change. So i tried :
call jrepl "^(.*?;.*?;.*?;).*" "$1" /f TEMP\*.csv /o -
I have an error message (incorrect name of file)... Any idea how to resolve it ?

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

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

#149 Post by foxidrive » 29 Jan 2016 14:25

You can wrap it in a for loop:

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /b /a-d "temp\*.csv" ') do (
call jrepl "^(.*?;.*?;.*?;).*" "$1" /f "temp\%%~a" /o -
)
pause

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

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

#150 Post by zimxavier » 31 Jan 2016 07:05

It works great. Thank you foxidrive :D

I hope Dave will accept to help me with my third situation, because i don't know if anybody else can.

Post Reply