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
mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

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

#166 Post by mirrormirror » 08 Mar 2016 21:13

Thank you - it mostly works after testing on one file but one line fails:

Code: Select all

call findrepl /o:13:13 < "%file%"|jrepl ".*" "SET \qmyVar2=$1\q" /x /i

produces:

Code: Select all

SET "myVar2=$1"SET "myVar2=$1"

Line 13 is:
"second.string here!"
- no "SET" or anything, just a string enclosed in quotes. The output needs to strip the quotes, then output this format
[SET "][myVAR2=][unquoted string][EndQuote]
like this:

Code: Select all

SET "myVAR2=second.string here!"

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

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

#167 Post by mirrormirror » 08 Mar 2016 21:33

Actually, I may have resolved it - it's been a while since I've used regex but I remembered a few things. - I changed the line from this:

Code: Select all

call findrepl /o:13:13 < "%file%"|jrepl ".*" "SET \qmyVar2=$1\q" /x /i

to:

Code: Select all

call findrepl /o:13:13 <"%file%" |jrepl "\q(.*)\q" "SET \qmyVar2=$1\q" /x /i

and I seem to be getting the correct output - at least from the command-line. I am going to test the whole thing on a few more files...

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

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

#168 Post by foxidrive » 08 Mar 2016 21:36

I edited my code on the last post too mirrormirror.

It's a little different and just removes the first and last character without checking for a quote.

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

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

#169 Post by zimxavier » 13 Mar 2016 11:07

Hi !

I have an issue with some lines with space(s) in excess:

call jrepl "(^\t)([a-zA-Z0-9_]*)" $2 /jmatch /f input.txt /o output.txt

input.txt

Code: Select all

   events = { 910 911 }
    action_clan_sentiment = {
   potential = {

output.txt
events
potential


I wish it extract "action_clan_sentiment" too, but there is a space before it (and after a tab). How can I write a code which catch all three like that :

events
action_clan_sentiment
potential


i.e. no take account of one or two space(s) in excess and take account of each tab.

PS: jrepl is very powerful, thank you :-)

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

#170 Post by dbenham » 13 Mar 2016 11:47

You are asking about basic regex functionality.

You can use the shorthand \w to represent a "word" character, equivalent to [a-zA-Z0-9_]

And you can use \s to represent whitespace, including space and tab.

I believe you should be looking for this pattern (in English):
  • Beginning of line
  • followed by 0 or more whitespace characters
  • followed by 1 or more "word" characters
  • followed by 0 or more whitespace characters
  • followed by =

And you want to capture the "word" characters, so you only need one set of parentheses:

Code: Select all

call jrepl "^\s*(\w+)\s*=" $1 /jmatch /f input.txt /o output.txt


Dave Benham

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#171 Post by Aacini » 13 Mar 2016 17:32

I am sorry, but I can't resist the temptation of post this reply... :roll:

Both dbenham's JREPL.BAT and my own FindRepl.bat programs are extensive and complex applications designed to perform advanced and very sophisticated text replacement operations. However, I never have seen that anyone of these programs be used at least at a small fraction of their capabilities! These programs are mainly sub-used, and the resources invested in they are wasted...

For example, although FindRepl.bat is very capable of extract the line # 13 of a file, IMHO it is just absurd to use a 950 lines program to achieve a result that can be obtained in a simpler way; like this one:

Code: Select all

for /F "usebackq skip=12 delims=" %%a in ("%file%") do set "line13=%%a" & goto break
:break

... or this one, that don't use a GOTO:

Code: Select all

set "line13="
for /F "usebackq skip=12 delims=" %%a in ("%file%") do if not defined line13 set "line13=%%a"


In a similar way, the replace operation shown in previous reply is finally executed in a very simple function in just one line of JScript code, so certainly you don't need a 850 lines program (like JREPL.BAT) in order to write and execute the same line in a small Batch-JScript hybrid file; for example:

Code: Select all

@set @a=0 //  &  cscript //nologo //E:JScript "%~F0"  &  goto :EOF

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/^\s*(\w+).+/gm,"$1\r\n"));

Output example:

Code: Select all

C:\> type input.txt
   events = { 910 911 }
    action_clan_sentiment = {
   potential = {

C:\> test.bat < input.txt
events
action_clan_sentiment
potential


You may review other examples of not-so-simple replacements that can be achieved in a few JSCript lines here or here...


:arrow: Nor JREPL.BAT neither FindRepl.bat can avoid that the user needs to learn regular expressions in order to successfully use these programs :!:

Antonio


PS - You may use the "Search an extra string in a block of lines (/B switch)" FindRepl's feature to perform a replacement in line 13 of the file:

Code: Select all

call findrepl "" /O:13:13 /Q:' /B:"'(.*)'" "SET 'myVar2=$1'" < "%file%"

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

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

#172 Post by zimxavier » 14 Mar 2016 06:23

Thanks to both of you :D
I have now a list of regex : https://msdn.microsoft.com/en-us/librar ... 80%29.aspx
It will take time to assimilate all of these regex, but it's very useful for my work already.

I have a new question. From input.txt :

Code: Select all

   option = {

      prestige = -20
      custom_tooltip = { text = no_own_ball_tooltip }
      name = EVTOPTB_REP_104
      wealth = -100
      narrative_event = { id = REP.105 days = 14 tooltip = EVTTOOLTIP_REP_105 }


I wish to extract all characters between "option =" and the end of the first following line containing "name ="

ouput.txt:

Code: Select all

   option = {

      prestige = -20
      custom_tooltip = { text = no_own_ball_tooltip }
      name = EVTOPTB_REP_104


I tried several codes but it doesn't work.
This one seems work only if "name" is in the line just after.

Code: Select all

call jrepl "option.*(\r\n)(.*name.*)" "$0" /jmatch /m /f input.txt /o output.txt


---------
@Aacini I found several posts about extract , remove or replace line #n but i would need to add a line. For example, a first line and/or a last line in a file.
Last edited by zimxavier on 14 Mar 2016 07:01, edited 2 times in total.

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

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

#173 Post by foxidrive » 14 Mar 2016 06:47

zimxavier wrote:I have a new question. Extract from input.txt :

Code: Select all

   option = {

      prestige = -20
      custom_tooltip = { text = no_own_ball_tooltip }
      name = EVTOPTB_REP_104
      wealth = -100
      narrative_event = { id = REP.105 days = 14 tooltip = EVTTOOLTIP_REP_105 }


I wish to extract all characters between "option =" and the end of the first following line containing "name ="


With Aacini's findrepl.bat

Code: Select all

call findrepl "option =" /e:"name.*" <"input.txt"





This uses a native Windows batch script called Findrepl.bat written by Aacini, which uses jscript to make it robust and swift.
viewtopic.php?f=3&t=4697

Place Findrepl.bat in the same folder as the batch file, or in a folder that is on the system path.

There is also copy on Dropbox (unblock it after downloading):
https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

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

#174 Post by dbenham » 14 Mar 2016 07:02

zimxavier wrote:I wish to extract all characters between "option =" and the end of the first following line containing "name ="

You need to bone up on your regular expression knowledge.

Code: Select all

call jrepl "^[ \t]*option =[\s\S]*?^\s*name =.*$" $0 /m /jmatch /f input.txt /o output.txt


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

#175 Post by dbenham » 27 Mar 2016 20:01

Here is version 3.8 with the following bug fixes.

1) Given certain options, I had some unintended global variables that could interfere with user defined JScript variables: i, libs, lib, and rtn. I moved them behind the _g opaque object.

2) Work around the %~f0 bug that crops up when a command is quoted

3) Substitute variable /OPTIONS for OPTIONS in the option parser so that it is unlikely to interfere with /V option variables.

JREPL3.8.zip
(9.01 KiB) Downloaded 722 times


Dave Benham

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

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

#176 Post by foxidrive » 28 Mar 2016 04:26

Nice one.

vincs2
Posts: 2
Joined: 05 Apr 2016 05:37

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

#177 Post by vincs2 » 05 Apr 2016 05:41

Hello thank you for this amazing work, I have the following question :
In a text file I have lines like that :

Code: Select all

C\MyFolder\Folder\File 31549115
C\MyFolder\Folder\File2 31544445
C\MyFolder\Folder\File3 31545555


and would like to look through the file and erase all the "C\MyFolder\" part to obtain something like that :

Code: Select all

Folder\File 31549115
Folder\File3 1549115
Folder\File2 31544445


Is that possible with your script?
Thanks !

vincs2
Posts: 2
Joined: 05 Apr 2016 05:37

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

#178 Post by vincs2 » 05 Apr 2016 07:00

Hello thank you for this amazing work, I have a question :

I have a txt file with paths of files and their size at every lines similar to this :

Code: Select all

\MyFolder\Folder\File01 31549115 
\MyFolder\Folder\File02 22512271 
\MyFolder\Folder\File03 19837047
 


I want to delete the \MyFolder\ part that it look like the following :

Code: Select all

Folder\File01 31549115 
Folder\File02 22512271
Folder\File03 19837047


Is it possible with your script ?

Thank you

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

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

#179 Post by foxidrive » 05 Apr 2016 07:40

This works with your sample text, and is very literal with what you've shown. It can be made more general, but it would be useful to give a sample of actual text format, if it is different.

The - means to overwrite the original file, so use a sample file to test it with.

Code: Select all

call jrepl "^..*?\\" "" /f "textfile.txt" /o -

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

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

#180 Post by catalinnc » 07 May 2016 12:20

@dbenham

1st thanks a lot for this very useful script...

i need a little help...

please, be kind and provide a line that will replace this characters with a space...

Code: Select all

=
%
"
^
!
<
>
&
|


if this is posible all in one line it will be perfect...

input file is "input.txt" and output file is "output.txt"...

thanks a lot...
_

Post Reply