JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets
Moderator: DosItHelp
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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.
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.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
The backslash has special meaning. You need to double it.
C:\\foobar
C:\\foobar
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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!
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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?
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?
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
Dreamling wrote:type %1 | jrepl "(\d{6})(\d{6})(\d{4})" "$1xxxxxx$3" /J
Take off the /J and run some tests
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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?
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?
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
Thanks for help!
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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.
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:
--OUTPUT--
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
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
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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!
foxidrive pointed me here and a combination of dbenham and foxi's batches helped me with my issue.
Thank you both!
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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
Inserts a new tag after
This command will remove tag added from above
Extract hr or min to the TIME_TABLE variable.
Extract the actual numer to TO_RUN variable.
Any help would be greatly appreciated!
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!
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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)
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.
Remove all <sysproperty/> tags
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.
Dave Benham
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
-
- Posts: 129
- Joined: 08 Feb 2016 20:25
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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:
And I need them to look like this: - text in (parentheses) are not needed in new 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.
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.
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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/> tagsCode: 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!
Re: JREPL.BAT - regex text processor - successor to REPL.BAT
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%"