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

#256 Post by dbenham » 01 Nov 2016 19:32

I found my first bug in v6.3 - I forgot to negate the result of the /EXC tests, so it was giving me an inverted result.

I fixed the bug and updated the prior post to v6.4.


Dave Benham

Jon
Posts: 2
Joined: 08 Nov 2016 15:56

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

#257 Post by Jon » 08 Nov 2016 16:37

Hi Dave,
Awesome work,

I am able to get the very basics of this working but I am by no means advanced enough to get what I am looking for to work.
I am wondering if it is possible to take multiple pieces of data from one xml and repopulate the tags in another xml. The catch is though that there is multiple tags in the same file with the same name.

Code: Select all

<example id="[b]aaa[/b]">
    <source id="0">
        <name>[b]bbb[/b]</name>
        <location>[b]ccc[/b]</location>
    </source>
    <size value="[b]ddd[/b]" />
</example>

<example id="[b]eee[/b]">
    <source id="1">
        <name>[b]fff[/b]</name>
        <location>[b]ggg[/b]</location>
    </source>
    <size value="[b]hhh[/b]" />
</example>

<example id="[b]iii[/b]">
    <source id="2">
        <name>[b]jjj[/b]</name>
        <location>[b]kkk[/b]</location>
    </source>
    <size value="[b]lll[/b]" />
</example>


Thank you for any help,
Jon.

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

#258 Post by dbenham » 09 Nov 2016 05:42

Regardless what your full requirements are, you really should use a tool designed to work with XML. The way you describe your problem, it sounds like a classic transformation problem for XSLT.

That being said, it might be possible with JREPL if you make a number of assumptions about the physical layout of your XML files (usually a bad idea). Unless I am misinterpreting your goal, I can almost guarantee it would require a fair amount of user supplied JScript, and I'm thinking you would be driving a nail with a screwdriver.

You haven't provided nearly enough information to begin to solve your problem with any tool. When you say "repopulate the tags in another xml", I can't tell if you are talking about merging two existing XML documents, or if your are building the second one from scratch based on the content of the first. You would have to provide a lot more details about the before and after documents.

I strongly encourage you to consider a true XML processing tool.


Dave Benham

Jon
Posts: 2
Joined: 08 Nov 2016 15:56

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

#259 Post by Jon » 09 Nov 2016 11:00

Hi Dave,
Thanks for the reply.

Sorry for not being very clear. I have two XML files both with information in, in the same structure.
What I am looking to do is replace the data in some of the tags on one XML file with data from the other XML file.
If it's not easy to do, I'll looking into using a proper XML editing tool as you say.

Thanks,
Jon.

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

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

#260 Post by Squashman » 09 Nov 2016 11:46

Jon wrote:Hi Dave,
Thanks for the reply.

Sorry for not being very clear. I have two XML files both with information in, in the same structure.
What I am looking to do is replace the data in some of the tags on one XML file with data from the other XML file.
If it's not easy to do, I'll looking into using a proper XML editing tool as you say.

Thanks,
Jon.

Without providing some clear verifiable examples it is hard to code anything at this point. We can probably give you an XML solution as well because we do have people here who use Vbscript and Jscript and both of those scripting languages have XML capability. But you need to provide more information before anyone can help you.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#261 Post by aGerman » 09 Nov 2016 11:53

Jon

It's Batch that can't handle XML properly. Even regex as Dave uses in his tool can't handle XML properly. It's not a big deal to write a hybrid script to solve your problem, see ...
viewtopic.php?f=3&t=7296
... but we would have to know the the entire structure of these XML files, beginning with the declaration <?XML ...

Since that would be pretty off topic in this thread I'd suggest you to create a new topic where you can provide the missing information.

Steffen

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

#262 Post by dbenham » 13 Nov 2016 22:10

JREPL6.6.zip
(18.97 KiB) Downloaded 776 times

JREPL version 6.6 introduces one new feature, refines another, and corrects a documentation error:

1) New /RTN option writes the result to an environment variable instead of stdout or a file. The entire result must fit within a single variable, else the command will fail. This option can be used with any combination of options, but it is probably most useful with /MATCH, /JMATCH, or /JMATCHQ if input is stdin or a file. This option is also an excellent way to manipulate the content of an existing variable if combined with /S and /M.

Output is first written to a temporary file within %TEMP%, and then it is read back in and stored in the variable, and the temp file is deleted.

The /RTN option is nearly useless if JREPL is used with a pipe because piped commands run within a new command shell process, and any variables defined within that process will be undefined as soon as the pipe terminates.

Here is the full documentation:

Code: Select all

>jrepl /?/rtn

      /RTN ReturnVar[:[-]LineNumber]

            Write the result to variable ReturnVar.

            If the optional LineNumber is present, then only that specified
            line within the result set is returned. A LineNumber of 1 is the
            first line. A negative LineNumber is measured from the end of the
            result set, so -1 is the last line.

            All byte codes except NULL (0x00) are preserved, regardless
            whether delayed expansion is enabled or not. An error is thrown
            and no value stored if the result contains NULL.

            An error is thrown and no value stored if the value does not fit
            within a variable. The maximum returned length varies depending
            on the variable name and result content. The longest possible
            returned length is 8179 bytes.

            The line terminator of the last match is suppressed if /MATCH,
            /JMATCH, or /JMATCHQ is used.

Example 1: Store the last word of input.txt in variable lastWord.

Code: Select all

call jrepl \S+ "" /match /f input.txt /rtn lastWord:-1

Example 2: Substitute : for all = characters within variable var (very difficult task with pure batch).

Code: Select all

call jrepl "=" ":" /m /s var /rtn var


2) Provide consistent support for extended ASCII when /X is used

JREPL was designed to primarily work with 7bit ASCII. However, it has always had limited support for high order bytes (extended ASCII). Byte codes >= 128 (0x80) were preserved when they appeared in stdin, input files, and/or output files. However, prior versions of JREPL did not properly read extended ASCII within command line parameters or variables. The only recourse was to use the \xnn string literal escape sequence, and /X was required to get the correct interpretation.

Version 6.5 modifies the behavior of /X so that command line arguments and variables are automatically written to temporary files within %TEMP%, where the extended ASCII string literals can be read properly. The temporary files are deleted once the command completes.

Now JREPL with /X will work properly with extended ASCII literals no matter where they apper. Note that environment variables actually store strings as UTF-16. It is possible that a variable may have a character that does not map to the current active code page, in which case batch will automatically convert it into a ? character. So care should be taken to make sure that all your content fits within the active code page.

3) Documentation correction

Version 6.0 introduced a documentation error regarding return codes. It improperly listed return codes of 0, 1, 3, and 4. The error persisted through version 6.5.

This version 6.6 fixes the documentation error and properly lists return codes 0, 1, 2, and 3.


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

#263 Post by dbenham » 24 Dec 2016 11:42

I've replaced version 6.5 with 6.6 in the previous post. The only difference is a correction to the documentation on return codes.


Dave Benham

Mike D
Posts: 6
Joined: 18 Aug 2016 19:38
Location: Canberra, Australia

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

#264 Post by Mike D » 27 Dec 2016 18:01

dbenham wrote:Example 1: Store the last word of input.txt in variable lastWord.

Code: Select all

call jrepl \s+ "" /match /f input.txt /rtn lastWord:-1


'\s' should be '\S' (upper case).

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

#265 Post by dbenham » 27 Dec 2016 21:23

Of course it should :oops:

I've edited the post to fix that mistake. Thanks Mike D.


Dave Benham

bluesky088
Posts: 4
Joined: 18 Jun 2016 21:21

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

#266 Post by bluesky088 » 26 Feb 2017 00:05

I'm new to JREPL.bat.

Hoping to replace a particular line in a text file. I'm currently using the following command to replace line 3 with the text abc123

jrepl "^.*" "abc123" /inc 3 /f "C:\test.txt" /o -

It works at the moment. However, if I wanted to replace line 3 with "abc123" (i.e. line 3 should also have quotation marks around abc123), any ideas how to do it?

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

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

#267 Post by Squashman » 26 Feb 2017 00:12

Did you look at the /X option.

bluesky088
Posts: 4
Joined: 18 Jun 2016 21:21

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

#268 Post by bluesky088 » 26 Feb 2017 05:36

Squashman wrote:Did you look at the /X option.


I'm not too sure how does the /X option work?

jrepl "^.*" "abc123" /inc 3 /f "C:\test.txt" /o - /X \q

Says there are too many arguments

bluesky088
Posts: 4
Joined: 18 Jun 2016 21:21

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

#269 Post by bluesky088 » 26 Feb 2017 05:45

bluesky088 wrote:
Squashman wrote:Did you look at the /X option.


I'm not too sure how does the /X option work?

jrepl "^.*" "abc123" /inc 3 /f "C:\test.txt" /o - /X \q

Says there are too many arguments


Figured it out:

jrepl "^.*" "\qabc123\q" /inc 3 /f "C:\test.txt" /o - /X

This works now. thanks

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

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

#270 Post by catalinnc » 01 Mar 2017 12:45

@dbenham

i have this text file:

Code: Select all

somerandomblablablaSTARTmorerandomblablablaENDlastrandomblablabla


can i use this tool (in a .bat) to insert a new line (hex sequence 0x0d0a) before START and after END in one ore more steps?
_

Post Reply