FindRepl.bat:New regex utility to search and replace strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: FindRepl.bat:New regex utility to search and replace str

#46 Post by foxidrive » 21 Nov 2014 05:40

Aacini wrote:PS - There is a new answer at S.O. that show a very clear example of /O and /B switches usage; perhaps you want to review it. It is here.


Thanks Antonio, that's useful and explains the /$ switch for me.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: FindRepl.bat:New regex utility to search and replace str

#47 Post by Aacini » 21 Nov 2014 09:29

bars143 wrote:Aacini , its working with "/J" switch but i dont know that your machine produced blanks as compared to my windows xp sp3 32it.

Bars


Note that your code execute these 4 commands:

Code: Select all

      type inputfile.txt |FindRepl "View" /a "displayed"

      type inputfile.txt |FindRepl "View" /a "displayed"

      type inputfile.txt |FindRepl "view" /a "displayed"

      type inputfile.txt |FindRepl "view" /a "displayed"

That is, no one of the commands have the /i switch! My modified code execute these commands:

Code: Select all

      type inputfile.txt |FindRepl /i "View" /a "displayed"

      type inputfile.txt |FindRepl "View" /a "displayed"

      type inputfile.txt |FindRepl /i "view" /a "displayed"

      type inputfile.txt |FindRepl "view" /a "displayed"

Sometimes I don't clearly express myself... :?

IMPORTANT: Note that in the "solution" the replacement string is enclosed in quotes and apostrophes: FindRepl /i "view" /a "'displayed'" /J; this is required because "displayed" (in quotes) is a literal string, but "'displayed'" (in quotes and apostrophes) is a JScript expression!

Antonio

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: FindRepl.bat:New regex utility to search and replace str

#48 Post by bars143 » 21 Nov 2014 17:27

Aacini wrote:
bars143 wrote:Aacini , its working with "/J" switch but i dont know that your machine produced blanks as compared to my windows xp sp3 32it.

Bars


Note that your code execute these 4 commands:

Code: Select all

      type inputfile.txt |FindRepl "View" /a "displayed"

      type inputfile.txt |FindRepl "View" /a "displayed"

      type inputfile.txt |FindRepl "view" /a "displayed"

      type inputfile.txt |FindRepl "view" /a "displayed"

That is, no one of the commands have the /i switch! My modified code execute these commands:

Code: Select all

      type inputfile.txt |FindRepl /i "View" /a "displayed"

      type inputfile.txt |FindRepl "View" /a "displayed"

      type inputfile.txt |FindRepl /i "view" /a "displayed"

      type inputfile.txt |FindRepl "view" /a "displayed"

Sometimes I don't clearly express myself... :?

IMPORTANT: Note that in the "solution" the replacement string is enclosed in quotes and apostrophes: FindRepl /i "view" /a "'displayed'" /J; this is required because "displayed" (in quotes) is a literal string, but "'displayed'" (in quotes and apostrophes) is a JScript expression!

Antonio


Aacini, thanks for your clarification. its really my typos error in posting code but your "'displayed'" really had a apostrophes which i cant see yesterday. i reviewed it today and see it has apostrophes! :mrgreen:

anyway i had another problem in my new script here:

inputfile.txt
there are two doublecolon : symbols : here
there are two doublequotes marks "symbols" here
there are less and greater than < symbols > here
there are questionmark and asterisk ? symbols * here
there are backslash and slash \ symbols / here
there are pipe and exclaimation marks | symbols ! here


my script here:

Code: Select all

@echo off
echo deleting characters:

type inputfile.txt |FindRepl ":|'|<|>|\?|\*|\\|/|\||\!" /A "" /Q:'

echo replacements:

type inputfile.txt |FindRepl ":|'|<|>|\?|\*|\\|/|\||\!" /A "[DC]|[DQ]|[LS]|[GR]|[QM]|[ST]|[BS]|[SL]|[PI]|[EM]" /Q:'

pause
exit


output here:

Code: Select all

deleting characters:


there are  two doublecolon  symbols  here
there are  two doublequotes marks symbols here
there are  less and greater than  symbols  here
there are  questionmark and asterisk  symbols  here
there are  backslash and slash  symbols  here
there are  pipe and exclaimation marks  symbols  here

replacements:


there are  two doublecolon [DC] symbols [DC] here
there are  two doublequotes marks [DQ]symbols[DQ] here
there are  less and greater than [LS] symbols [GR] here
there are  questionmark and asterisk  symbols  here
there are  backslash and slash [PI] symbols [SL] here
there are  pipe and exclaimation marks  symbols  here

Press any key to continue . . .


as shown above:

1) "[QM]" , "[ST]" , "[BS]" , "[EM]" are not displayed.

2) "[PI]" is misplaced and is found in "[BS]" position.

and i do know how to script correctly.

Bars

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

Re: FindRepl.bat:New regex utility to search and replace str

#49 Post by foxidrive » 21 Nov 2014 17:55

Bars, did you describe and provide the output that you are trying to get?

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: FindRepl.bat:New regex utility to search and replace str

#50 Post by bars143 » 21 Nov 2014 18:20

foxidrive wrote:Bars, did you describe and provide the output that you are trying to get?


foxi,

i want a question mark symbol "?" displayed as "[QM]"

i want a asterisk symbol "*" displayed as "[ST]"

i want a backslash symbol "\" displayed as "[BS]"

i want a exclamation mark symbol "!" displayed as "[EM]"

here is supposed output to be displayed (replacement part only):

Code: Select all

there are  two doublecolon [DC] symbols [DC] here
there are  two doublequotes marks [DQ]symbols[DQ] here
there are  less and greater than [LS] symbols [GR] here
there are  questionmark and asterisk [QM] symbols [ST] here
there are  backslash and slash [BS] symbols [SL] here
there are  pipe and exclaimation marks [PI] symbols [EM] here


in my previous post my script output display is (replacement part only) :

Code: Select all

there are  two doublecolon [DC] symbols [DC] here
there are  two doublequotes marks [DQ]symbols[DQ] here
there are  less and greater than [LS] symbols [GR] here
there are  questionmark and asterisk  symbols  here
there are  backslash and slash [PI] symbols [SL] here
there are  pipe and exclaimation marks  symbols  here


my previous script is:

Code: Select all

@echo off
echo deleting characters:

type inputfile.txt |FindRepl ":|'|<|>|\?|\*|\\|/|\||\!" /A "" /Q:'

echo replacements:

type inputfile.txt |FindRepl ":|'|<|>|\?|\*|\\|/|\||\!" /A "[DC]|[DQ]|[LS]|[GR]|[QM]|[ST]|[BS]|[SL]|[PI]|[EM]" /Q:'

pause
exit

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

Re: FindRepl.bat:New regex utility to search and replace str

#51 Post by foxidrive » 21 Nov 2014 19:00

To simplify this problem:

This doesn't work because the ? character can't be escaped in the way I tried it below.

Code: Select all

echo a?bcd|findrepl "\?|b|c|d" /A "[QM]|[ST]|[BS]|[EM]"


and it needs to be escaped somehow because this generates an error:

Code: Select all

echo a?bcd|findrepl "?|b|c|d" /A "[QM]|[ST]|[BS]|[EM]"

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: FindRepl.bat:New regex utility to search and replace str

#52 Post by Aacini » 21 Nov 2014 19:34

This is a subtle bug. I need to carefully review the program in order to find it. However, the cure is simple and the same than before: change the version1 literal-to-literal replacement by the new version 2 regexp-to-JScript replacement:

Code: Select all

C:\> echo a?bcd|findrepl "\?||b||c||d" /A "'[QM]'||'[ST]'||'[BS]'||'[EM]'" /J
a[QM][ST][BS][EM]


Antonio

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: FindRepl.bat:New regex utility to search and replace str

#53 Post by bars143 » 21 Nov 2014 20:57

Aacini, thanks for answer and now my new script is:

Code: Select all

@echo off

echo INDIVIDUAL REPLACEMENT:

type inputfile.txt |FindRepl "\|" "'[PI]'" /J >outputfile.txt
type outputfile.txt |FindRepl "\!" "'[EM]'" /J >outputfile.txt
type outputfile.txt |FindRepl "\*" "'[ST]'" /J >outputfile.txt
type outputfile.txt |FindRepl "\?" "'[QM]'" /J >outputfile.txt
type outputfile.txt |FindRepl "\\" "'[BS]'" /J >outputfile.txt
type outputfile.txt |FindRepl "\/" "'[SL]'" /J >outputfile.txt
type outputfile.txt

echo ALTERNATION REPLACEMENT:

type outputfile.txt |FindRepl ":|'|<|>|\*" /A "[DC]|[DQ]|[LS]|[GR]|[ST]" /Q:'

pause
exit



inputfile.txt :

Code: Select all



there are  two doublecolon : symbols : here
there are  two doublequotes marks "symbols" here
there are  less and greater than < symbols > here
there are  questionmark and asterisk ? symbols * here
there are  backslash and slash \ symbols / here
there are  pipe and exclaimation marks | symbols ! here



resulting output in console :

Code: Select all

INDIVIDUAL REPLACEMENT:


there are  two doublecolon : symbols : here
there are  two doublequotes marks "symbols" here
there are  less and greater than < symbols > here
there are  questionmark and asterisk [QM] symbols [ST] here
there are  backslash and slash [BS] symbols [SL] here
there are  pipe and exclaimation marks [PI] symbols [EM] here

ALTERNATION REPLACEMENT:


there are  two doublecolon [DC] symbols [DC] here
there are  two doublequotes marks [DQ]symbols[DQ] here
there are  less and greater than [LS] symbols [GR] here
there are  questionmark and asterisk [QM] symbols [ST] here
there are  backslash and slash [BS] symbols [SL] here
there are  pipe and exclaimation marks [PI] symbols [EM] here

Press any key to continue . . .


solve !

Bars

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

Re: FindRepl.bat:New regex utility to search and replace str

#54 Post by foxidrive » 21 Nov 2014 22:13

It can be done in one pass, Bars, but the first term using a pipe needs special handling, using findstr V2.0

Code: Select all

type inputfile.txt |findrepl "(\|)||!||\*||\?||\\||/||:||'||<||>" /A "'[PI]'||'[EM]'||'[ST]'||'[QM]'||'[BS]'||'[SL]'||'[DC]'||'[DQ]'||'[LS]'||'[GR]'" /J /Q:'


EDIT: The first term with pipe works fine when it is placed at the end of the search terms, and this seems to be because three ||| in a row is what causes the problem.
Here the two leading terms in search and replace have been shifted to the last position without any special handling:

Code: Select all

type inputfile.txt |findrepl "!||\*||\?||\\||/||:||'||<||>||\|" /A "'[EM]'||'[ST]'||'[QM]'||'[BS]'||'[SL]'||'[DC]'||'[DQ]'||'[LS]'||'[GR]'||'[PI]'" /J /Q:'

output

Code: Select all

there are  two doublecolon [DC] symbols [DC] here
there are  two doublequotes marks [DQ]symbols[DQ] here
there are  less and greater than [LS] symbols [GR] here
there are  questionmark and asterisk [QM] symbols [ST] here
there are  backslash and slash [BS] symbols [SL] here
there are  pipe and exclaimation marks [PI] symbols [EM] here

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: FindRepl.bat:New regex utility to search and replace str

#55 Post by bars143 » 21 Nov 2014 22:42

foxidrive wrote:It can be done in one pass, Bars, but the first term using a pipe needs special handling, using findstr V2.0

Code: Select all

type inputfile.txt |findrepl "(\|)||!||\*||\?||\\||/||:||'||<||>" /A "'[PI]'||'[EM]'||'[ST]'||'[QM]'||'[BS]'||'[SL]'||'[DC]'||'[DQ]'||'[LS]'||'[GR]'" /J /Q:'



thanks Foxi , you got it solved !.

but we have the same scripts except those "(\|)" , where my console report an error : expected ")"... but i cant figure it out what it really means?

when i tried to put a ")" anywhere i chose - something next error is expected "/" then put a "/" anywhere then expected ";" error? got no solution , then i decided to go individual replacement rather than solving this error message where i am no knowledge about error code.

anyway a second thanks,

Bars

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

Re: FindRepl.bat:New regex utility to search and replace str

#56 Post by dbenham » 21 Nov 2014 23:10

It is very simple with JREPL v2.

As literal values:

Code: Select all

jrepl  ": \q < > ? * \ / | !"  "[DC] [DQ] [LS] [GR] [QM] [ST] [BS] [SL] [PI] [EM]"  /l /x /t " " /f input.txt

Or as a regular expressions:

Code: Select all

jrepl ": \q < > \? \* \\ / \| !" "[DC] [DQ] [LS] [GR] [QM] [ST] [BS] [SL] [PI] [EM]" /x /t " " /f input.txt


Dave Benham

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: FindRepl.bat:New regex utility to search and replace str

#57 Post by bars143 » 21 Nov 2014 23:20

dbenham wrote:It is very simple with JREPL v2.

As literal values:

Code: Select all

jrepl  ": \q < > ? * \ / | !"  "[DC] [DQ] [LS] [GR] [QM] [ST] [BS] [SL] [PI] [EM]"  /l /x /t " " /f input.txt

Or as a regular expressions:

Code: Select all

jrepl ": \q < > \? \* \\ / \| !" "[DC] [DQ] [LS] [GR] [QM] [ST] [BS] [SL] [PI] [EM]" /x /t " " /f input.txt


Dave Benham


i though that jrepl has no alternation like findrepl

thanks for suggestion Dave its working too.

Bars

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

Re: FindRepl.bat:New regex utility to search and replace str

#58 Post by foxidrive » 22 Nov 2014 06:11

Aacini and dbenham, you two blokes have created brilliant tools with these Jscript enhanced batch files.
If I had any sway in Microsoft then I'd get them included in a Windows Update patch so that administrators and enthusiasts everywhere can benefit.

Below I have a suggestion to help people use your tools, because there is a fair amount of chatter and questions in the main threads,
if you both think it is worthwhile then I'd suggest that you create dedicated threads called something like:

FindRepl: Examples and tutorials
FindRepl: Code
FindRepl: Discussion

Jrepl: Examples and tutorials
Jrepl: Code
Jrepl: Discussion


The current threads can be renamed to the : Discussion threads.

In the Example threads you'd keep all the samples of code, and explanations of how they can be used
A Code thread can have a history of the code releases with a LARGE message at the top to see the last post for the most recent version, and all linked appropriately to the other threads.
- and the mods here can move any posts with conversations/questions to the Discussion threads.

It might make the threads easier to maintain, plus searchers can browse the appropriate threads for the valuable tips and tricks that many of us need to effectively use your gems.
There's some initial work to get it set up but I'm also willing to help if needed.

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

Re: FindRepl.bat:New regex utility to search and replace str

#59 Post by foxidrive » 22 Nov 2014 06:13

Bars, see my previous post today regarding the pipe and special handling in FindRepl, as it turns out there is another way of arranging the terms:
viewtopic.php?p=38183#p38183

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

Re: FindRepl.bat:New regex utility to search and replace str

#60 Post by brinda » 22 Nov 2014 08:05

foxidrive wrote:Aacini and dbenham, you two blokes have created brilliant tools with these Jscript enhanced batch files.
If I had any sway in Microsoft then I'd get them included in a Windows Update patch so that administrators and enthusiasts everywhere can benefit.

Below I have a suggestion to help people use your tools, because there is a fair amount of chatter and questions in the main threads,
if you both think it is worthwhile then I'd suggest that you create dedicated threads called something like:

FindRepl: Examples and tutorials
FindRepl: Code
FindRepl: Discussion

Jrepl: Examples and tutorials
Jrepl: Code
Jrepl: Discussion


The current threads can be renamed to the : Discussion threads.

In the Example threads you'd keep all the samples of code, and explanations of how they can be used
A Code thread can have a history of the code releases with a LARGE message at the top to see the last post for the most recent version, and all linked appropriately to the other threads.
- and the mods here can move any posts with conversations/questions to the Discussion threads.

It might make the threads easier to maintain, plus searchers can browse the appropriate threads for the valuable tips and tricks that many of us need to effectively use your gems.
There's some initial work to get it set up but I'm also willing to help if needed.


i second foxidrive's suggestion. both are marvelous tools with so much features built in..

Post Reply