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

Post Reply
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

#91 Post by dbenham » 18 Jun 2015 07:09

I may have the column positions off a bit, but here is a looooong one liner that very efficiently creates your final CSV using only a single FOR loop executing wget piped to JREPL twice :)

Code: Select all

@echo off
TITLE Get WORS Results

SET /P "RACENUM=What race number do you want to retrieve? : "
set "url=http://www.wors.org/results/2015"

(for %%G in (junior) do wget -O - -q %url%/%RACENUM%/overall/%%G.htm |^
jrepl "^((?:(  DNF|   NR)|.{5}).{80})(.*Broken Spoke.*)" ^
      "$1+($2?'           ':'')+$3+'\x22%RACENUM%\x22,\x22%%G\x22'" ^
      /j /a |^
jrepl "(.{5})(.{9})(..)(.{6})(.{7})(.{26})(.{26})(...)(.{12})(.{12})(.{32})(.*)" ^
      "f($1)+f($2)+f($3)+f($4)+f($5)+f($6)+f($7)+f($8)+f($9)+f($10)+f($11)+$12" ^
      /j /jbeg "function f(str){return str.replace(/^\s*(\S(.*\S)*)*/,'\x22$1\x22,')}"
)>>ALL_Race_Results.csv

EDIT - Simplified the regex in the f(str) function


Dave Benham

pyr0ball
Posts: 3
Joined: 18 Jun 2015 10:19

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

#92 Post by pyr0ball » 18 Jun 2015 10:33

Hey guys, I'm having a very strange issue with JREPL not catching a seemingly simple string. I'm unsure if I'm using improper syntax, or if JREPL has some other issues when editing *.reg files.

I'm starting from a pre-made registry file where I've replaced a portion of a registry key with the string CURRENT_DIRECTORY, and I'm trying to have JREPL replace that with a variable for the current working directory. I've used this technique successfully for other parts of my script where it's replacing text in *.ini and *.txt files, but it cant seem to detect it in this case. Here's the full line and the command I'm calling:

Code: Select all

"SFXModule"="CURRENT_DIRECTORY\\WinRAR-Portable\\*.sfx"


Code: Select all

C:\Test> jrepl.bat "CURRENT_DIRECTORY" "TESTOUT" /m /f Default_SplitSFXProfile.reg /o SplitSFXProfile.reg


After running, the output file looks exactly the same:

Code: Select all

"SFXModule"="CURRENT_DIRECTORY\\WinRAR-Portable\\*.sfx"


Any ideas what I may be doing wrong?

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

#93 Post by dbenham » 18 Jun 2015 14:21

pyr0ball wrote:After running, the output file looks exactly the same:
...
Any ideas what I may be doing wrong?

Are you sure your input file is ASCII (ANSI) and not unicode?


Dave Benham

pyr0ball
Posts: 3
Joined: 18 Jun 2015 10:19

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

#94 Post by pyr0ball » 18 Jun 2015 14:41

dbenham wrote:Are you sure your input file is ASCII (ANSI) and not unicode?


Dave Benham


Notepad++ reports the input file as UCS-2 Little Endian, and the same for the output

pyr0ball
Posts: 3
Joined: 18 Jun 2015 10:19

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

#95 Post by pyr0ball » 18 Jun 2015 14:45

dbenham wrote:
pyr0ball wrote:After running, the output file looks exactly the same:
...
Any ideas what I may be doing wrong?

Are you sure your input file is ASCII (ANSI) and not unicode?


Dave Benham


Well, I guess that was my issue. Converted to ANSI first and was able to get a proper output file! Now I just have to see if regedit will take an ANSI .reg instead of unicode

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

#96 Post by dbenham » 15 Jul 2015 16:14

Here is a version 3.6 - a very minor release that simply adds the /?? option for paged help via MORE

JREPL.BAT Version 3.6
JREPL3.6.zip
(8.78 KiB) Downloaded 975 times


Dave Benham

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

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

#97 Post by foxidrive » 15 Aug 2015 07:14

Dave,

We discussed . not matching \n I've misunderstood something here and I am unsure where I've gone wrong.


I thought this term would match the end of a line, any string of numerals, and then the end of that line.

"\r\n([0-9]*)\r\n"

My aim is to have all the numbers that are on lines by themselves, to be on one line - and this code does that, but it has other effects of removing more line endings.

Code: Select all

@echo off
(
echo(Thanks for playing, here are the details of your purchase.
echo(
echo(Game: $600 Million Jackpot
echo(Draw(s^): 11562
echo(Purchased: 5 Aug, 2015
echo(Ticket No: 0 055 43
echo(Entry Details: 1 game
echo(Price: $1.20 (includes $0.10 commission^)
echo(
echo(Your Numbers
echo(Game 1
echo(3
echo(15
echo(29
echo(31
echo(32
echo(39
echo(45
)>file1.txt


call jrepl "\r\n([0-9]*)\r\n" " $1 " /m /x /f file1.txt
pause



This is the output and the numerals are on one line as I wish, but below that is the input text where you can see more changes have taken place.
How does the regular expression do this? I'm stumped.


Thanks for playing, here are the details of your purchase. Game: $600 Million Jackpot
Draw(s): 11562
Purchased: 5 Aug, 2015
Ticket No: 0 055 43
Entry Details: 1 game
Price: $1.20 (includes $0.10 commission) Your Numbers
Game 1 3 15 29 31 32 39 45




Thanks for playing, here are the details of your purchase.

Game: $600 Million Jackpot
Draw(s): 11562
Purchased: 5 Aug, 2015
Ticket No: 0 055 43
Entry Details: 1 game
Price: $1.20 (includes $0.10 commission)

Your Numbers
Game 1
3
15
29
31
32
39
45

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

#98 Post by dbenham » 15 Aug 2015 07:59

* matches 0 or more times, so \r\n([0-9]*)\r\n empty lines as well as lines containing only digits.

The solution is to use + (match 1 or more times) instead of *

Code: Select all

call jrepl "\r\n([0-9]+)\r\n" " $1 " /m /x /f file1.txt


Dave Benham

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

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

#99 Post by foxidrive » 15 Aug 2015 08:13

dbenham wrote:The solution is to use + (match 1 or more times) instead of *

Dave Benham


Thank you for enlightening me Dave. :)

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

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

#100 Post by foxidrive » 19 Aug 2015 00:27

Dave, this isn't really a bug but it's something that may benefit from a rethink wrt /x switches

What happens is when a .csv file is passed to jrepl - and assume double quotes are required -
then a letter in the path is changed to a formfeed in C:\files\apple carrot\pie

It will affect paths like d:\twitter and f:\backup too with tab and backspace, if I got it right.

Code: Select all

@echo off
SET "encrypted=C:\files\apple carrot\pie"

echo receipt_20150814.csv|jrepl ".*(........)...." "echo \q%encrypted%\$1\q"  /x >"temp.txt"

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

#101 Post by dbenham » 19 Aug 2015 06:14

That behavior is to be expected, and is necessary. Your string includes \f, which is interpreted by the /X option as a form-feed character.

When using the /X option, all \ literals should be escaped as \\. Technically you need only escape any \ literal that precedes a character that would be recognized as an escape sequence. But it is a good idea to simply escape all the \ literals.

Note how in addition to escaping the variable value, I also escape the one \ in the replacement string.

Code: Select all

@echo off
setlocal
set "encrypted=C:\\files\\apple carrot\\pie"

echo receipt_20150814.csv|jrepl ".*(........)...." "echo \q%encrypted%\\$1\q"  /x >"temp.txt"


Dave Benham

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

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

#102 Post by foxidrive » 19 Aug 2015 07:24

EDITED:

I've modified my reply as I realise now that it's not as widespread as I had assumed, Dave.

it doesn't affect the contents of $& such as below, which I had assumed would suffer too.

I just noticed - the formfeed is at the start and the end of each line.


d:\files>dir cap* /b /s /a-d |jrepl ".*" "\f$&" /x
♀d:\files\capture - 2015-08-16_161106.jpg♀
♀d:\files\capture - 2015-08-18_190558.jpg♀
♀d:\files\capture - 2015-08-18_190558.png♀


dbenham wrote:Note how in addition to escaping the variable value, I also escape the one \ in the replacement string.


That escape doesn't affect the sample code I posted right, but would if there was the affected text characters after the backslash.

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

#103 Post by dbenham » 19 Aug 2015 10:27

You fell into the trap of .* matching an empty string (again :wink: ) :!:

.* first matches the entire line, then it matches the empty string before the line end. The solution is to add the ^ anchor.

Code: Select all

dir cap* /b /s /a-d |jrepl "^.*" "\f$&" /x

Or you could simplify to:

Code: Select all

dir cap* /b /s /a-d |jrepl "^" "\f" /x


Dave Benham

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

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

#104 Post by foxidrive » 19 Aug 2015 19:41

dbenham wrote:You fell into the trap of .* matching an empty string (again :wink: ) :!:


hehe Wasn't it with /m that we discussed it not matching a \n ?

.* first matches the entire line, then it matches the empty string before the line end. The solution is to add the ^ anchor.


I'm aware of anchoring to the line start and end but I'm really surprised because GnuSED doesn't act this way - see here for sed output:

Code: Select all

d:\files>dir cap* /b|jrepl ".*" "a"
aa
aa
aa

d:\files>dir cap* /b|sed s/.*/a/g
a
a
a


Thanks for your explanation once more Dave - it's most useful.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

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

#105 Post by brinda » 10 Sep 2015 18:30

dave,

a request for word jumble mix for jrepl.

currently using excel code here for jumble and modifying them the suit the needs
http://www.mrexcel.com/forum/excel-questions/37340-word-scramble.html


Normal word list input on text file looks below

Code: Select all

Sri Advaita
Bhagavad Gita
Saptaham
Maha Bali Puram


Processed list text file (Normal, jumble,mirror,reverse)

Code: Select all

Normal,jumble,mirror,reverse

Sri Advaita,rSi vAdiata,atiavdA irS,irS atiavdA
Bhagavad Gita,avBgahd tiGa,atiG davagahB,davagahB atiG
Saptaham,tpahaaSm,mahatpaS,mahatpaS
Maha Bali Puram,ahMa lBai arPum,maruP ilaB ahaM,ahaM ilaB maruP 


Jumble1 mix criteria,
a)Only the last letter in a word should remain in its original position.
b)Maintain Letter Capitalization
c)Original position of space should remain

Jumble2 Mirror criteria
a)Reversal of letters position and the word position from left to right. E.g "Sri Advaita" becomes "atiavdA irS"
b)Maintain Letter Capitalization
c)Original position of space should remain

Jumble3 Reverse criteria
a)Reversal of letters position from left to right. Word position remains. E.g "Sri Advaita" becomes "irS atiavdA"
b)Maintain Letter Capitalization
c)Original position of space should remain

thanks,
brinda

Post Reply