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

#106 Post by dbenham » 10 Sep 2015 22:51

Conceptually it is quite simple if you only do one form of manipulation at a time. Your replacement script can be a JScript expression, so all that is needed are some functions to reverse and/or scramble a string.

There are built in functions that make reversing a string simple enough that you can embed the functions directly within the replace string.

mirror.bat

Code: Select all

@call jrepl ".+" "$0.split('').reverse().join('')" /j /f input.txt /o mirror.txt

reverse.bat

Code: Select all

@call jrepl "\w\w+" "$0.split('').reverse().join('')" /j /f input.txt /o reverse.txt


Randomly scrambling a string is more complex. In this case it makes sense to define a scramble function and then use it within the replace string. One option for defining a custom function is to use /JBEG.

jumble.bat via /JBEG

Code: Select all

@call jrepl "(\w\w+)(\w)" "scramble($1)+$2" /j /f input.txt /o jumble.txt ^
      /jbeg "function scramble(s){s=s.split('');for(var i = s.length,j,k;i;j=parseInt(Math.random()*i),k=s[--i],s[i]=s[j],s[j]=k); return s.join('');}"

That looks pretty ugly. There are ways to clean it up, but there is an easier way that is also reusable. You can create a file containing one or more functions and include the definition using /JLIB.

scramble.jlib

Code: Select all

function scramble(s) {
  s=s.split('');
  for(
    var i=s.length, j, temp;
    i;
    j=parseInt(Math.random()*i), temp=s[--i], s[i]=s[j], s[j]=temp
  );
  return s.join('');
}

jumble.bat via /JLIB

Code: Select all

@call jrepl "(\w\w+)(\w)" "scramble($1)+$2" /j /jlib scramble.jlib /f input.txt /o jumble.txt


Now for the tricky part - producing all 4 results in one pass as you have shown in your sample output. I got creative on this :)
Have fun figuring out how it works :twisted:

combined.bat (reusing scramble.jlib)

Code: Select all

@echo off
setlocal
set "rev=split('').reverse().join('')"
>combined.txt (
  echo normal,jumble,mirror,reverse
  echo(
  call jrepl "(\w*)(\w)|\W+"^
             "rev+=$0.%rev%;scramble($2)+$3|rev+=$0,$0"^
             /jbegln "norm=$txt; rev=''"^
             /jendln "$txt=norm+','+$txt+','+norm.%rev%+','+rev"^
             /jlib scramble.jlib /j /t "|" /f input.txt
)


Dave Benham

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

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

#107 Post by brinda » 11 Sep 2015 02:08

dave,

thank you :D

your mirror.bat and reverse.bat gives some overview how the character are being manipulated/ I usually save the whole thread and foxidrive's previous question/result topic helps a little more on this.

Anything after that gives me more :!: and :?: and probably going to melt the brain :roll:

thanks for helping on this

staghavi
Posts: 1
Joined: 29 Oct 2015 17:37

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

#108 Post by staghavi » 29 Oct 2015 17:49

Thank u for this great script,
But there is a problem: unfortunately I couldn't use some characters like , or ; as DelimiterChar.

my command was:

Code: Select all

JREPL "autoreport: true;autolaunch: true;proxyall: false" "autoreport: false;autolaunch: false;proxyall: true" /f "lantern-2.0.10 - Copy.yaml" /o - /l /m /t ;


Can you fix it? (or at least mention it in the help)

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

#109 Post by dbenham » 30 Oct 2015 15:06

The solution is already mentioned in the second paragraph of the help :wink:

Code: Select all

  Each parameter may be optionally enclosed by double quotes. The double
  quotes are not considered part of the argument. The quotes are required
  if the parameter contains a batch token delimiter like space, tab, comma,
  semicolon. The quotes should also be used if the argument contains a
  batch special character like &, |, etc. so that the special character
  does not need to be escaped with ^.


So all you need to do is quote the semicolon delimiter:

Code: Select all

JREPL "autoreport: true;autolaunch: true;proxyall: false" "autoreport: false;autolaunch: false;proxyall: true" /f "lantern-2.0.10 - Copy.yaml" /o - /l /m /t ";"


Dave Benham

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

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

#110 Post by Nuvolarix » 11 Jan 2016 03:40

Hi Dave, this batch looks so cool, I'm trying to use it but I'm in trouble with multi-line and/or newline functions... I cannot figure out how to properly use /M /X and I guess mainly... \n :roll: :cry:

This the exact string (multi-line) I should search for (included spaces and newlines):

Code: Select all

        <file name="App On">
          <icon>icon.ico</icon>
          <path>App.exe</path>
          <Autorun>2</Autorun>

and this is exactly how it should be replaced:

Code: Select all

        <file name="App Off">
          <icon>icon.ico</icon>
          <path>App.exe</path>

Any help would be greatly appreciated! :)

Thanks in advance.

Best Regards
Nuv

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

#111 Post by dbenham » 11 Jan 2016 09:30

You have to remember that \r may be present as well.

I'm assuming your search does not care about exact spacing, but that you want to remove the one line and preserve all other spacing.

I would use the following:

Code: Select all

jrepl "(^\s*<file name=\qApp On\q>\s*<icon>icon.ico</icon>\s*<path>App.exe</path>[ \t]*\r?\n)[[ \t]*<Autorun>2</Autorun>[ \t]*\r?\n" $1 /m /x /f test.txt /o -


Dave Benham

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

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

#112 Post by Nuvolarix » 12 Jan 2016 02:08

You are right and to be precise I would like to remove that line in batch-1 and vice versa add it back in a batch-2 (which I was assuming to easily get by inverting the 'search' and 'replace' fields of batch-1 but now I guess it needs something different as well).

Reading your code I must admit \r wasn't my only trouble, unfortunately I'm still not able to achieve the goal, I'm not sure if I'm doing something wrong just testing your code:

1) I've created a test.txt file:

Code: Select all

        <file name="App On">
          <icon>icon.ico</icon>
          <path>App.exe</path>
          <Autorun>2</Autorun>

2) I run your code in a batch (this batch, jrepl.bat and test.txt in the same directory)

The file is actually edited but it shows no differences, "Autorun" line is still there... :roll:

Many thanks for your help :)

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

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

#113 Post by thefeduke » 12 Jan 2016 12:11

Nuvolarix wrote:2) I run your code in a batch (this batch, jrepl.bat and test.txt in the same directory)
The file is actually edited but it shows no differences, "Autorun" line is still there...
I set up your test like this (Revised: input data and solution):

Code: Select all

@Echo Off &SetLOCAL &Rem.and keep environment uncluttered.
Rem.DOS Testing Environment '0' easy to find temporary folder
    Set "tmpd=%TEMP%\'0'"
    If Not Exist "%tmpd%" MkDir "%tmpd%"
::
    (
        Echo.        ^<file name="App X"^>   
        Echo.          ^<icon^>iconX.ico^</icon^>
        Echo.          ^<path^>AppX.exe^</path^>
        Echo.          ^<Autorun^>2^</Autorun^>
        Echo.        ^<file name="App Off"^>     
        Echo.          ^<icon^>icon.ico^</icon^>   
        Echo.          ^<path^>App.exe^</path^>   
        Echo.          ^<Autorun^>2^</Autorun^>
        Echo.        ^<file name="App Y"^>   
        Echo.          ^<icon^>iconY.ico^</icon^>
        Echo.          ^<path^>AppY.exe^</path^>
        Echo.        ^<file name="App Z"^>   
        Echo.          ^<icon^>iconZ.ico^</icon^>
        Echo.          ^<path^>AppZ.exe^</path^>
        Echo.          ^<Autorun^>3^</Autorun^>
    )>"%tmpd%\%~n0_ToRemove.txt"
    Echo.
    Echo.Input:
    Type "%tmpd%\%~n0_ToRemove.txt"
    Call jrepl "(^\s*<file name=\qApp Off\q>\s*<icon>icon.ico</icon>\s*<path>App.exe</path>[ \t]*\r?\n)[[ \t]*<Autorun>2</Autorun>[ \t]*\r?\n?" $1 /m /x /f "%tmpd%\%~n0_ToRemove.txt" /o -
    Echo.
    Echo.Modified:
    Call jrepl "App Off" "App On" /m /x /f "%tmpd%\%~n0_ToRemove.txt"
::  Do it again to store the output 
    Call jrepl "App Off" "App On" /m /x /f "%tmpd%\%~n0_ToRemove.txt" /o "%tmpd%\%~n0_ToInsert.txt"
    Echo.
    Echo.Replenished:
    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 -
    Call jrepl "App On" "App Off" /m /x /f "%tmpd%\%~n0_ToInsert.txt"
::  Do it again to store the output 
    Call jrepl "App On" "App Off" /m /x /f "%tmpd%\%~n0_ToInsert.txt" /o -
GoTo :EOF
The autorun is restored at the end and you can tweak the input and JRepl in the above self-contained script.
Edit: Output example from previous attempt deleted from post.
Dave, I admire your work very much and thank you for this example as a learning model. It does exactly what you said it would. To change the string "App On" to "App Off" in addition as Nuv suggested, would you just use a second pass of JRepl?
John A.
Last edited by thefeduke on 15 Jan 2016 16:30, 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

#114 Post by dbenham » 12 Jan 2016 13:46

@Nuvolarix - I did test, and everything worked for me.

My best guess is your test file ends with the <Autorun> line, and the last line is not terminated with \n.

My posted code assumes the <Autorun> line ends with \n, so it would not match if it doesn't.

The problem is easily fixed by making the \n optional with the ? quantifier:

Code: Select all

jrepl "(^\s*<file name=\qApp On\q>\s*<icon>icon.ico</icon>\s*<path>App.exe</path>[ \t]*\r?\n)[[ \t]*<Autorun>2</Autorun>[ \t]*\r?\n?" $1 /m /x /f test.txt /o -


Dave Benham

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

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

#115 Post by Nuvolarix » 13 Jan 2016 03:26

dbenham wrote:My best guess is your test file ends with the <Autorun> line, and the last line is not terminated with \n.
Dave Benham

It was exactly like that, I'm sorry I didn't understand by myself :oops: and thank you for the fix!
You also were assuming right, the <Autorun> line ends with \n, except in my stupid test file.

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

    ( 
        Echo.          ^<Autorun^>2^</Autorun^>
    )>>%tmpd%\test.txt
because my entire file looks like this:

Code: Select all

        <file name="App X">
          <icon>iconX.ico</icon>
          <path>AppX.exe</path>
          <Autorun>2</Autorun>
          ...
->      <file name="App On">      <-
->        <icon>icon.ico</icon>   <-
->        <path>App.exe</path>    <-
          ...
        <file name="App Y">
          <icon>iconY.ico</icon>
          <path>AppY.exe</path>
        <file name="App Z">
          <icon>iconZ.ico</icon>
          <path>AppZ.exe</path>
          <Autorun>3</Autorun>
          ...
          etc.

I was hoping to have learnt something but it looks once again I'm not able to do it by myself, at the moment it looks bigger than me :cry:

Could you help me for this final step too?
Once again your help would be very much appreciated.

Best Regards
Nuv

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

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

#116 Post by Nuvolarix » 14 Jan 2016 05:00

ok, not sure it's the best way to achieve the goal, but here a code which looks working:

Code: Select all

jrepl "(^\s*<file name=\qApp On\q>\s*<icon>icon.ico</icon>\s*<path>App.exe</path>[ \t]*\r?\n)" "\r\n        <file name=\qApp On\q>\r\n          <icon>icon.ico</icon>\r\n          <path>App.exe</path>\r\n          <Autorun>2</Autorun>\r\n" /m /x /f test.txt /o -
Thank you Dave for the help and the patience and obviously for your awesome batch 8)

P.S.: one thing I'm not able to get rid of... what about if I have to replace a "["?
I get "JScript runtime error in Search regular expression: Expected ']' (...)" and a new empty file test.txt.new is created.

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

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

#117 Post by Squashman » 14 Jan 2016 07:18

Nuvolarix wrote:P.S.: one thing I'm not able to get rid of... what about if I have to replace a "["?
I get "JScript runtime error in Search regular expression: Expected ']' (...)" and a new empty file test.txt.new is created.

Use the hex code.

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

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

#118 Post by Nuvolarix » 14 Jan 2016 09:25

Thank you for reply @Squashman.

Got it!

The problem was the use of option /x :?

Code: Select all

jrepl "\x5b" ok /x /f test.txt /o -

Code: Select all

jrepl "\u005b" ok /x /f test.txt /o -
They both give "JScript runtime error in Search regular expression: Expected ']' (...)"

Code: Select all

jrepl "\x5b" ok /f test.txt /o -

Code: Select all

jrepl "\u005b" ok /f test.txt /o -
They both give "ok" 8)

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

#119 Post by dbenham » 14 Jan 2016 11:19

Ooh, that is an interesting an unplanned difference between /X vs. no /X. I'll have to do some more research and decide if that is a bug, and if it is, see if I can fix it.

But the simplest way to search for [ when using a regular expression is to escape it. The escape works both with and without \X.

Code: Select all

D:\test>ECHO [1]|jrepl "\[" "" /x
1]

D:\test>ECHO [1]|jrepl "\[" ""
1]


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

#120 Post by dbenham » 14 Jan 2016 22:33

That was definitely a bug - thanks for finding and reporting that problem Nuvolarix.

I had a bug where a regex search could fail if it contained \xnn or \unnnn that evaluated to a meta-character, and the /X option was used.
The fix was quite simple, and it actually simplified the code :-)

Besides the bug fix, I also reworked the error processing a bit in a transparent way.

So here is JREPL.BAT version 3.7
JREPL3.7.zip
(8.85 KiB) Downloaded 796 times


Dave Benham

Post Reply