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
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#31 Post by foxidrive » 07 Dec 2014 04:04

Greetings Dave!

I'd like to pad the numbers at the beginning of a line with leading whitespace.

Code: Select all

4117:hea$der.log
307:inputfile.txt
307:inputfile2.txt
716:IP.bat
288:outputfile.txt


Can you please show an example of how I would get this, with a field width of 6 characters and the : replaced with a -

Code: Select all

  4117-hea$der.log
   307-inputfile.txt
   307-inputfile2.txt
   716-IP.bat
   288-outputfile.txt


EDIT: I'm almost there. Just trying to find out how to add the minus sign

Code: Select all

echo 123:abc def|jrepl "(.*?):(.*)" "lpad($1,'      ')+$2" /j


All Good!

Code: Select all

echo 123:abc def|jrepl "(.*?):(.*)" "lpad($1,'      ')+'-'+$2" /j

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

#32 Post by dbenham » 07 Dec 2014 08:58

Nothing to fear, right :?: :wink:

Another way:

Code: Select all

echo 123:abc def|jrepl "^(.*?):" "lpad($1,'      ')+'-'" /j


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

#33 Post by dbenham » 09 Dec 2014 08:08

I updated my version 3.1 post to version 3.2 (also updated the top current version post).

There was a major bug with the /T option when /JMATCH was not used - the dynamic replace function was missing a set of {}. I don't understand how any of my test cases ever worked, but somehow they did. But there was definitely a bug that needed squashing.

I also added a GOTO at the top to improve startup performance. It now skips the documentation without wasting time parsing.


Dave Benham

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

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

#34 Post by foxidrive » 09 Dec 2014 19:51

Thanks Dave.

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

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

#35 Post by Squashman » 17 Dec 2014 19:04

Could JREPL be used to remove CR\LF pairs from a file and output it to a new file without them?

I know I can do this with a vbscript by doing a READALL and then use a REPLACE on vbCrLf and then use the WRITE method to output it to a new file.

As most of you know I do use some rather large files sometimes. So not sure if this is necessarily the best way to do it. I would assume if I pipe or redirect to JREPL.bat it wouldn't have issues with files larger than 2GB's?

For a good third party utility to do this I use TR.exe but I always like the native option if possible.

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

#36 Post by dbenham » 17 Dec 2014 20:51

If the file is < 2 gigabytes, then you could use:

Code: Select all

jrepl "\r\n" "" /m /f input.txt /o output.txt
This one is fairly obvious. It loads the entire file into a single string variable, hence the 2 gigabyte limit. Using a pipe doesn't change anything. The multi-line option still must load the entire input into one string variable.


If the file is > 2gigabytes, then:

Code: Select all

jrepl "^" "" /jEndLn "output.Write($txt); $txt=false;" /f input.txt /o output.txt
This one doesn't replace anything, but it uses custom JScript to write out each line without the newline, and then disables the normal write that includes newline.

This second solution can handle any size file as long as no line exceeds 2 gigabytes. But afterwards, output.txt cannot be processed by JREPL.BAT because it consists of a single line > 2 gigabytes.


Dave Benham

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

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

#37 Post by Squashman » 17 Dec 2014 21:08

The first one is very similar to using TR.

Code: Select all

tr -d '\r\n' <filein.txt >fileout.txt

The second one looks similar in the way vbscript does a write versus a write line.

I should do some timed testing to see which is fastest. I will do your two methods, vbscript and then TR.

This will come in handy. Thanks Dave.

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

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

#38 Post by foxidrive » 18 Dec 2014 00:11

I'd be interested in seeing the speed test results, Squashman.

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

#39 Post by dbenham » 18 Dec 2014 07:03

I'll make a speed test prediction, from fastest to slowest:

1) tr
2) dedicated VBScript or JScript
3) JREPL option 1 (up until it fails at 2 GB)
4) JREPL option 2

But I should think the IO would be the overwhelming bottle neck, with none of the options having any advantage there. So I'm guessing all options will be within ~25% of each other, perhaps even more tightly packed.


Dave Benham

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

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

#40 Post by Squashman » 18 Dec 2014 08:28

I will do all the testing on my Hard drive versus the network drive. We don't even have a Gigabit Lan and some of our servers are offsite hosted by another company. We are lucky if we get 40Mb of bandwidth to the offsite servers.

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

#41 Post by dbenham » 24 Dec 2014 12:31

I updated my version 3.2 post to version 3.3 (also updated the top current version post).

I fixed a bug that occurred when /JMATCH was combined with /M or /S. The bug was introduced in version 3.0. The bug would never have seen the light of day if I had a proper regression test plan. One of these days...

old line 659:

Code: Select all

      if (!alterations || str1!=str2) if (multi) {
became new line 660:

Code: Select all

      if (!jmatch) if (!alterations || str1!=str2) if (multi) {

old line 673:

Code: Select all

      if (!alterations) output.Write(_g.fmtNum(ln,lnPad)+str2);
became new line 674:

Code: Select all

      if (!jmatch) output.Write(_g.fmtNum(ln,lnPad)+str2);


Dave Benham

skantanka
Posts: 1
Joined: 29 Dec 2014 18:14

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

#42 Post by skantanka » 30 Dec 2014 19:03

How do you replace three double qoutes (""") with two double quotes ("")?

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

#43 Post by dbenham » 13 Jan 2015 22:02

You cannot pass a double quote to any CSCRIPT program via the command line. So you must use an escape sequence - either \q or \x22. If used in the replacement string, then you must use the /X option. If \q is used in the search string, then you must use the /X option. But /x22 is OK in the search string without the /X option.

There are many ways to substitute 2 quotes for 3. Here are a few options:

Code: Select all

jrepl \q\q\q \q\q /x /f test.txt
jrepl \x22{3} \x22\x22 /x f test.txt
jrepl "(\q\q)\q" $1 /x f test.txt
jrepl "(\x22{2})\x22" $1 f test.txt


Dave Benham

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

Re: JREN.BAT - Rename files/folders using regular expression

#44 Post by foxidrive » 21 Jan 2015 00:12

Dave, I've been processing data and it occurred to me that your tool would be useful if it could also process a text file,
plus change the case of a certain letter in a certain word - for example:

change and capitalise the first letter:


give me all your money, and don't sound the alarm!


to this


Give me all your money, and don't sound the alarm!


but someone may want to capitalise the third letter of the 5th word and the 2nd letter of the last word


give me all your moNey, and don't sound the aLarm!



Do you think there is a solution in your range of tools for something like this?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: JREN.BAT - Rename files/folders using regular expression

#45 Post by dbenham » 21 Jan 2015 14:52

That is exactly the type of problem that JREPL.BAT is designed to solve. The /J option provides a mechanism to apply the JScript toUpperCase() method to a portion of the replacement string.

Your question really belongs in the JREPL.BAT thread.

Assuming your text is in file test.txt:

1) Capitalize the first letter

Code: Select all

jrepl "^." "$0.toUpperCase()" /j /f test.txt /o -


2) Capitalize 3rd letter of 5th word and 2nd letter of last word. I perform this in two steps because there may not be a 5th word, but there is always a last word (except for empty lines). Also, the 5th word might also be the last word.

Code: Select all

call jrepl "^(\s*(?:\S+\s+){4}\S{2})(\S)" "$1+$2.toUpperCase()" /j /f test.txt /o -
call jrepl "((?:^|\s)\S)(\S)(\S*\s*)$" "$1+$2.toUpperCase()+$3" /j /f test.txt /o -

The output of the first JREPL can be piped as input into the second, but then the output file must be a temporary file, followed by a MOVE.

Code: Select all

jrepl "^(\s*(?:\S+\s+){4}\S{2})(\S)" "$1+$2.toUpperCase()" /j /f test.txt | jrepl "((?:^|\s)\S)(\S)(\S*\s*)$" "$1+$2.toUpperCase()+$3" /j /o test.txt.new
move /y test.txt.new test.txt >nul


Dave Benham

Post Reply