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
jcherney
Posts: 2
Joined: 11 Feb 2016 10:32

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

#151 Post by jcherney » 11 Feb 2016 10:43

Dave et al,

Please bear with me as I am not a programmer by trade. Just enough to get me in trouble! I have a text file with errant data created by a program where the programmer doesn't exist anymore to ask.

I am trying to replace this: C:\foobar with Jrepl. I can replace foobar and c:, but the \ is causing me all kinds of problems. I've tried just plain \ , I've tried "\", I've tried it using the whole string with and without quotes. Nothing. I'm guessing that it might have to do with using the literal command, but I'm not sure how to do that (if that's even correct).

What am I missing?

Many thanks in advance. This program is awesome.

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

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

#152 Post by Squashman » 11 Feb 2016 11:56

The backslash has special meaning. You need to double it.
C:\\foobar

jcherney
Posts: 2
Joined: 11 Feb 2016 10:32

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

#153 Post by jcherney » 11 Feb 2016 13:41

Squashman wrote:The backslash has special meaning. You need to double it.
C:\\foobar


Thanks. So you mean it should read (acutal code) jrepl C:\\nocover.txt "-s changetext /f faxbatch.bat /o -


edit: It worked! Thanks so much for your help!

Dreamling
Posts: 3
Joined: 15 Feb 2016 04:49

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

#154 Post by Dreamling » 15 Feb 2016 05:00

Hi!
Could you help me, please?

I need to do a simple operation on every string from file, thats contains 16 decimal characters one by one, and then replace 6 characters from position 7 to 13, for example, string:
sometext1234567890123456sometext
must be:
sometext123456xxxxxx3456sometext
after replacement.
In this case, i tried to use your script, but i cant fill the replacement section correctly.
Here is what i try to do:
type %1 | jrepl "(\d{6})(\d{6})(\d{4})" "$1xxxxxx$3" /J
But it doesnt works.
The compiler doesnt know what is $1xxxxxx$3. How i can escape variables in replacement secion?

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

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

#155 Post by foxidrive » 15 Feb 2016 15:31

Dreamling wrote:type %1 | jrepl "(\d{6})(\d{6})(\d{4})" "$1xxxxxx$3" /J


Take off the /J and run some tests

Dreamling
Posts: 3
Joined: 15 Feb 2016 04:49

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

#156 Post by Dreamling » 16 Feb 2016 02:26

OMG, that works! Thank you very much! :)
Can u explain my mistake pls?
I read in help section, that /J key allows me to operate with search matched substrings as a variables:
::: /J - The Replace argument is a JScript expression.
::: The following variables contain details about each match:
:::
::: $0 is the substring that matched the Search
::: $1 through $n are the captured submatch strings
::: $off is the offset where the match occurred
::: $src is the original source string

Does it mean, that this rules works every time, not only with /J key?

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

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

#157 Post by foxidrive » 16 Feb 2016 04:04

Dreamling wrote:OMG, that works! Thank you very much! :)
Can u explain my mistake pls?
I read in help section, that /J key allows me to operate with search matched substrings as a variables:
::: /J - The Replace argument is a JScript expression.


Jrepl has the ability to use advanced jscript commands in the command line, as well as the earlier features of repl.bat - in this case the jscript functionality isn't needed.

Dreamling
Posts: 3
Joined: 15 Feb 2016 04:49

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

#158 Post by Dreamling » 16 Feb 2016 06:46

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

#159 Post by dbenham » 16 Feb 2016 07:22

Demonstrating what foxidrive was referring to - If you wanted to use the /J option, then the replacement string must be a valid JScript expression that evaluates to a string result. In this case, you want the value of variable $1 concatenated with a string constant, followed by the value of variable $3. Enclosing double quotes are not included in the expression, they simply quote the argument for the batch script.

Code: Select all

echo sometext1234567890123456sometext|jrepl "(\d{6})(\d{6})(\d{4})" "$1+'xxxxxx'+$3" /J

But, as foxidrive said, there is no reason to use /J in your case.

Your find/replace can be improved. I don't think you want to modify a string of digits that is 17 or more digits long. You can use some more advanced regex to easily accomplish this. Also, there is no need to capture the middle 6 digits since you are replacing them with a string constant. In fact, there is no need to capture the final 4 digits either, you can use a look-ahead feature instead.

Here is a more robust solution. The first 3 lines should all be modified, and the last 3 should not:

Code: Select all

@echo off
for %%A in (
  sometext1234567890123456sometext
  1234567890123456sometext
  sometext1234567890123456
  sometext12345678901234567sometext
  12345678901234567sometext
  sometext12345678901234567
) do echo %%A|jrepl "((?:^|\D)\d{6})\d{6}(?=\d{4}(?!\d))" "$1xxxxxx"

--OUTPUT--

Code: Select all

sometext123456xxxxxx3456sometext
123456xxxxxx3456sometext
sometext123456xxxxxx3456
sometext12345678901234567sometext
12345678901234567sometext
sometext12345678901234567

Breaking it down:

(?:^|\D) matches either the beginning of a line, or a non-digit. The ?: simply signifies that the expression should not be captured. However, it is included in a larger expression that is captured: ((?:^|\D)\d{^}), which captures either 6 digits at the beginning of a line, or else a non-digit, followed by 6 digits.

\d{6} simply matches the 6 digits you want to overwrite.

?= at the beginning of a parenthesized expression is the look-ahead feature. It means the prior expression only matches if it is followed by a string that matches the contents of the parentheses. But the matching content is not considered to be part of the match that gets replaced. ?! is nearly the same, except it is a negative lookahead, meaning the prior content fails to match if it is followed by the specified content.

So (?=\d{4}(?!\d)) means the matched text must precede 4 digits that are not followed by a 5th digit.


Dave Benham

ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

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

#160 Post by ArieS » 17 Feb 2016 14:57

Just wanted to thank Dave for this batch file.
foxidrive pointed me here and a combination of dbenham and foxi's batches helped me with my issue.
Thank you both!

yamaduc
Posts: 2
Joined: 01 Mar 2016 20:57

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

#161 Post by yamaduc » 01 Mar 2016 21:10

Hi Dave,

I'm trying to port some sed command over using jrepl.

Great script btw!

Here's some of the commands I'm trying to do in sed but wonder if it's possible with jrepl.

The sed command below

Code: Select all

sed -i -e "s|\(<.*junit.*printSummary.*>\)|\1\<sysproperty key\=\"exitTestTag\" value\=\"@$EXIT_TEST_WITH_TAG\"\/>|g" build.xml


Inserts a new tag after

Code: Select all

<junit printSummary="yes" showoutput="true" fork="true" haltonerror="false" maxmemory="${memory}" >



This command will remove tag added from above

Code: Select all

sed -i -e 's|<sysproperty key\=\"exitTestTag\" value\=\".*\"\/>||' build.xml



Extract hr or min to the TIME_TABLE variable.
Extract the actual numer to TO_RUN variable.

Code: Select all

# Check whether to run the test for hr or min
        TIME_TABLE=`echo $TESTS_TO_EXECUTE | sed -e 's/.*[[:digit:]]hr/hr/' -e 's/.*[[:digit:]]min/min/'`

        # Get the actual number
        TO_RUN=$(echo $TESTS_TO_EXECUTE | tr -cd '[[:digit:]]')



Any help would be greatly appreciated!

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

#162 Post by dbenham » 05 Mar 2016 10:27

You really should be using an XML utility to modify XML. Your sed code is not very robust. But if you insist on using a generic text editing utility, then yes, JREPL can do the job just as easily and efficiently (and as well or as poorly) as sed.

Insert <sysproperty/> tag within <junit><junit/> tag
I've attempted to improve your search string to more accurately search for a <junit> tag that contains a printSummary attribute. (untested)

Code: Select all

call jrepl "(<junit\s(?:[^>]*\s)?printSummary=.*?>)"^
           "$1<sysproperty key=\qexitTestTag\q value=\q@%EXIT_TEST_WITH_TAG%\q/>"^
           /x /f build.xml /o -

Your original code, and the above, will insert a new <sysproperty/> tag each time it is run, even if one already exists. A simple modification will replace any existing <sysproperty/> tag with updated %EXIT_TEST_WITH_TAG% value.

Code: Select all

call jrepl "(<junit\s(?:[^>]*\s)?printSummary=.*?>)(<sysproperty .*?/>)?"^
           "$1<sysproperty key=\qexitTestTag\q value=\q@%EXIT_TEST_WITH_TAG%\q/>"^
           /x /f build.xml /o -


Remove all <sysproperty/> tags

Code: Select all

call jrepl "<sysproperty key=\qexitTestTag\q value=\q.*?\q/>" "" /x /f build.xml /o -


Parse TESTS_TO_EXECUTE into TIME_TABLE and TO_RUN values
My interpretation of your code is you have a value formatted as "arbitrary text NNNhr arbitrary text" or "arbitrary text NNNmin arbitrary text", and you want to parse out the numeric value and the time unit. You don't show what the arbitrary text may look like. If it follows a pattern, then you probably can use simple batch commands to parse the values. But if the text is truly arbitrary, then the following JREPL code is a good solution.

Code: Select all

for /f "tokens=1,2" %%A in (
  'jrepl ".*?(\d+)(hr|min).*" "$1 $2" /a /s TESTS_TO_EXECUTE'
) do (
  set "TO_RUN=%%A"
  set "TIME_TABLE=%%B"
)


Dave Benham

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

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

#163 Post by mirrormirror » 08 Mar 2016 17:44

Hey guys, here's my next issue. I need transform some files or create new files using data from existing files. In searching for a solution, I read through the findrepl thread and am on page 9 of this jrepl thread and my mind is a bit burnt out at the moment...
I have not even attempted to code anything yet and am just asking for some advice/reccomendations.
My scenario:
I have several files in generally the same format.
- They are TEXT files
- They DO contain special characters &^%><! etc.
I need to extract some [text] from the first part of the file
Do minor manipulation of that [text]
Write a new file (or edit the existing one)
- with this extracted/manipulated [text] at the beginning
+ lines [54] until [EOF] from the old file

So my files currently look like this:

Code: Select all

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10 set "something=first string! here..."
line 11
line 12
line 13 "second.string here!"
line 14
line 15
line 16
line 17
....
line 54
.... (xxx number of lines)
END

And I need them to look like this: - text in (parentheses) are not needed in new file

Code: Select all

Heading:  first string! here... (from line 10)
Line 10 SET "myVar1=first string! here..." (from line 10)
Line 11 (as is)
SET "myVar2=second.string here!" (from line 13)
Line 17 (as is)
goto:EOF

line 54
...
Until end of file

So I'm not 100% sure I'm in the right thread - I know jrepl can do some (if not all) of this - maybe findrepl can also do it? I can do some of it with basic batch code but don't want to reinvent the wheel - and I'm still not sure of a "safe" way to write text to a new file using an existing text file containing an unknown number of poison characters - which I think either jrepl or findrepl can do easily.

yamaduc
Posts: 2
Joined: 01 Mar 2016 20:57

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

#164 Post by yamaduc » 08 Mar 2016 18:19

dbenham wrote:You really should be using an XML utility to modify XML. Your sed code is not very robust. But if you insist on using a generic text editing utility, then yes, JREPL can do the job just as easily and efficiently (and as well or as poorly) as sed.

Insert <sysproperty/> tag within <junit><junit/> tag
I've attempted to improve your search string to more accurately search for a <junit> tag that contains a printSummary attribute. (untested)

Code: Select all

call jrepl "(<junit\s(?:[^>]*\s)?printSummary=.*?>)"^
           "$1<sysproperty key=\qexitTestTag\q value=\q@%EXIT_TEST_WITH_TAG%\q/>"^
           /x /f build.xml /o -

Your original code, and the above, will insert a new <sysproperty/> tag each time it is run, even if one already exists. A simple modification will replace any existing <sysproperty/> tag with updated %EXIT_TEST_WITH_TAG% value.

Code: Select all

call jrepl "(<junit\s(?:[^>]*\s)?printSummary=.*?>)(<sysproperty .*?/>)?"^
           "$1<sysproperty key=\qexitTestTag\q value=\q@%EXIT_TEST_WITH_TAG%\q/>"^
           /x /f build.xml /o -


Remove all <sysproperty/> tags

Code: Select all

call jrepl "<sysproperty key=\qexitTestTag\q value=\q.*?\q/>" "" /x /f build.xml /o -


Parse TESTS_TO_EXECUTE into TIME_TABLE and TO_RUN values
My interpretation of your code is you have a value formatted as "arbitrary text NNNhr arbitrary text" or "arbitrary text NNNmin arbitrary text", and you want to parse out the numeric value and the time unit. You don't show what the arbitrary text may look like. If it follows a pattern, then you probably can use simple batch commands to parse the values. But if the text is truly arbitrary, then the following JREPL code is a good solution.

Code: Select all

for /f "tokens=1,2" %%A in (
  'jrepl ".*?(\d+)(hr|min).*" "$1 $2" /a /s TESTS_TO_EXECUTE'
) do (
  set "TO_RUN=%%A"
  set "TIME_TABLE=%%B"
)


Dave Benham


Thank you Dave. Made my life much better. Great tool!

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

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

#165 Post by foxidrive » 08 Mar 2016 19:42

mirrormirror wrote:I need transform some files or create new files using data from existing files. In searching for a solution, I read through the findrepl thread and am on page 9 of this jrepl thread and my mind is a bit burnt out at the moment...


This uses both findrepl and jrepl - it seems to work here.

Code: Select all

@echo off
set "file=file.txt"
(
call findrepl /o:10:10 < "%file%"|jrepl ".*?=(.*)." "Heading:  $1\r\nSET \qmyVar1=$1\q" /x /i
call findrepl /o:11:11 < "%file%"
call findrepl /o:13:13 < "%file%"|jrepl "^.(.*).$" "SET \qmyVar2=$1\q" /x /i
call findrepl /o:17:17 < "%file%"
echo goto:EOF
call findrepl /o:54    < "%file%"
)>"%file%".1
move /y "%file%".1 "%file%"

Post Reply