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

#91 Post by foxidrive » 27 Aug 2015 05:06

Hi Antonio,

When I tried to change three letters in the block using the /e: switch in findrepl V2.2 it replaced/removed the primary rSearch term instead.


V 2.2 returns this:

Code: Select all


555
bbb


V1.x returns this:

Code: Select all

aaa

bbb


Sample code

Code: Select all

@echo off
(
echo(aaa
echo(555
echo(bbb
)>filein.txt

type filein.txt|findrepl "aaa" /e:"bbb" /b:"555" ""

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

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

#92 Post by foxidrive » 16 Dec 2015 00:41

Hi Aacini!

Here's some sample code to make an issue clearer that is in the current findrepl.bat


findrepl July 7 2013 This is the correct output

AAA bbb
ccc ddd


findrepl 2.2 This is not right.
\r\nAAA bbb
ccc ddd



Sample code

Code: Select all

@echo off
(
echo(
echo(aaa bbb
echo(ccc ddd
echo(
)>file.txt

type file.txt|findrepl  "\r\naaa" "\r\nAAA"

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

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

#93 Post by foxidrive » 08 Mar 2016 20:29

Antonio, should findrepl be able to be chained on the command line?
I see this here:

c:\Files\Misc\+bat\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

c:\Files\Misc\+bat\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

c:\Files\Misc\+bat\findrepl.bat(784, 4) Microsoft JScript runtime error: Input past end of file


Code: Select all

@echo off
(
echo line 1
echo line 2
echo line 3
echo line 4
echo line 5
echo line 6
echo line 7
echo line 8
echo line 9
echo line 10 first string here...
echo line 11
echo line 12
echo line 13 second string here...
echo line 14
)>"file.txt"

:go

@echo off
set "file=file.txt"
call findrepl /o:10:10 < "%file%"|findrepl "^(.*)$" "Heading  $1" /R
call findrepl /o:13:13 < "%file%"|findrepl "^(.*)$" "Heading  $1" /R
pause
goto :go

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

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

#94 Post by foxidrive » 05 Apr 2016 13:30

I thought I had this straight when I checked the built-in help, it will print only the changed lines.
Did I get it wrong again?

Code: Select all

c:\Files>type file.txt|findrepl ".*NCName.*" "abc" /i /r
aaa
abc
zzz

c:\Files>type file.txt
aaa
111 uNcName 222
zzz

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

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

#95 Post by catalinnc » 07 May 2016 12:23

@Aacini

1st thanks a lot for this very useful script...

i need a little help...

please, be kind and provide a line that will replace this characters with a space...

Code: Select all

=
%
"
^
!
<
>
&
|


if this is posible all in one line it will be perfect...

input file is "input.txt" and output file is "output.txt"...


thanks a lot...
_

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

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

#96 Post by Aacini » 07 May 2016 18:39

@catalinnc, this works at the command-line prompt:

Code: Select all

FindRepl "[=%@^!<>&|]" "' '" /Q:@ /J < input.txt > output.txt

HOWEVER...

FindRepl.bat is an almost-1000-lines complex Batch file capable of perform very advanced replacement tasks, but your replacement request is a very simple one. Perhaps you would consider to use a much simpler 4-lines Batch-JScript hybrid script that perform the same task instead...

Code: Select all

@set @a=0  /*
@cscript //nologo //E:JScript "%~F0" < input.txt > output.txt
@goto :EOF */

WScript.Stdout.WriteLine(WScript.Stdin.ReadAll().replace(/[=%"\^!<>&\|]/g,' '));

Save this file with .bat extension.

Antonio

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

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

#97 Post by catalinnc » 08 May 2016 12:14

@Aacini thanks a lot for your support...much respect...
_

Code: Select all

FindRepl "[=%@^!<>&|]" "' '" /Q:@ /J < input.txt > output.txt


works perfect from command-line prompt...

please, can you mod it so it will work from a .bat file?
_

Code: Select all

@set @a=0  /*
@cscript //nologo //E:JScript "%~F0" < input.txt > output.txt
@goto :EOF */

WScript.Stdout.WriteLine(WScript.Stdin.ReadAll().replace(/[=%"\^!<>&\|]/g,' '));


works perfect from a .bat file!
_

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

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

#98 Post by thefeduke » 11 May 2016 23:56

catalinnc, Here is a one-liner that works with Prompt or .bat .

Code: Select all

call poison input.txt >output.txt
Although it functions within the limited scope that you described, it does break down if the character '!' is imbedded one or more times within a larger string. This is a middle approach that has no advantage other than to see if it could be done without going outside of plain batch script.

The above requires this file that I named Poison.bat :

Code: Select all

   @Echo Off
    SETLOCAL enabledelayedexpansion
    set "DelChars== %% ^" ^^^^ ^^! ^< ^> ^& ^| "
    set "DelChars=!DelChars: =!"
    For /F "usebackq tokens=* delims=" %%I in ("%~1") DO (
        set "str=A%%I"
        set "len=0"
        For /L %%A in (15,-1,0) do (
            set /a "len|=1<<%%A"
            for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
        )
        Set /A "pos=!len!-1"
        Set "PoisonString=%%I"
        IF "!PoisonString!" EQU "" (
            Set PoisonString=^^!
            set "pos=0"
        )
        For /L %%C in (0,1,8) do (
            Set "Base=!DelChars:~%%C,1!"
            For /L %%P in (!pos!,-1,0) do (
                Set "Test=!PoisonString:~%%P,1!"
                If /I "!Test!" EQU "!Base!" (
                    Set "Sterile= !Sterile!"
                ) Else (
                    Set "Sterile=!PoisonString:~%%P,1!!Sterile!"
                )
            )
            Set "PoisonString=!Sterile!"
            Set "Sterile="
        )
        If /I "%%I" EQU "" (
Rem.        Echo.Panic^^!
            Set "PoisonString= "
        )
        Echo.!PoisonString!
    )
Exit /B
John A.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

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

#99 Post by thefeduke » 25 May 2016 14:34

thefeduke wrote:catalinnc, ... Although it functions within the limited scope that you described, it does break down if the character '!' is imbedded one or more times within a larger string. This is a middle approach that has no advantage other than to see if it could be done without going outside of plain batch script.
I tried to build a preprocessor to handle the "!" and found that I could incorporate it right into the above. The solution is posted within Topic: "Replace specific line in a .txt with text" at http://www.dostips.com/forum/viewtopic.php?f=3&t=7167&p=46734#p46734 and it can handle the above as well as turning :

Code: Select all

The=teacher%angry:"Shut up!^Be<quiet>and&listen|to me!
into

Code: Select all

The teacher angry: Shut up. Be quiet and listen to me.

John A.

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

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

#100 Post by foxidrive » 25 Jul 2016 20:26

Hi guys, I'm after some feedback to ascertain if this problem is on my machine.

If you open a cmd prompt and run each of these scripts multiple times in a row - do you get an error message at all?

Both scripts should just show ccc on the console.
I find that I get an error message randomly. Sometimes it prints ccc just fine.


Code: Select all

@echo off
echo aaa|findrepl "aaa" "bbb"|more|findrepl "bbb" "ccc"


Code: Select all

@echo off
echo aaa|findrepl "aaa" "bbb"|findrepl "bbb" "ccc"


The second script doesn't work at all here - and I get different error messages.

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 strings

#101 Post by dbenham » 26 Jul 2016 06:00

I can't reproduce your problem - Both work just fine for me after repeated runs ('though I ran the command from the command line, not a script)


Dave Benham

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

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

#102 Post by thefeduke » 26 Jul 2016 16:07

This is Win7 Home Premium SP1 64-bit with findrepl at the front of my path not in CD.

Code: Select all

C:\Users\Zani\Scripts>foxibug2
ccc

C:\Users\Zani\Scripts>foxibug1
ccc


C:\Users\Zani\Scripts>foxibug2
ccc

C:\Users\Zani\Scripts>foxibug1
ccc


C:\Users\Zani\Scripts>foxibug2
ccc

C:\Users\Zani\Scripts>foxibug1
C:\Users\Zani\Programs\Functions\FindREPL.bat(318, 1) Microsoft JScript runtime error: Permission denied


C:\Users\Zani\Scripts>foxibug1
ccc


C:\Users\Zani\Scripts>foxibug1
ccc


C:\Users\Zani\Scripts>foxibug1
ccc


C:\Users\Zani\Scripts>

foxibug1.bat wrote:@echo off
:: viewtopic.php?p=48056#p48056
:: Post subject: Re: FindRepl.bat:New regex utility to search and replace strings
::Posted: Mon Jul 25, 2016 10:26 pm by foxidrive
echo aaa|findrepl "aaa" "bbb"|more|findrepl "bbb" "ccc"

I also have Win10 handy: one Win7 32-bit based and one Win8.1 based. Let me know, but I guess he above is something.
John A.

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

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

#103 Post by foxidrive » 26 Jul 2016 21:36

Thank you John and thanks Dave.

The error you saw here John is the same error I see.

thefeduke wrote:This is Win7 Home Premium SP1 64-bit with findrepl at the front of my path not in CD.

Code: Select all

C:\Users\Zani\Scripts>foxibug2
ccc

C:\Users\Zani\Scripts>foxibug1
C:\Users\Zani\Programs\Functions\FindREPL.bat(318, 1) Microsoft JScript runtime error: Permission denied


Thanks for your tests, John.

Mine is Windows 8.1 32 bit and findrepl is on the path here. Running this from a cmd prompt.
My tests are below

z-more.bat

Code: Select all

@echo off
echo aaa|findrepl "aaa" "bbb"|more|findrepl "bbb" "ccc"


z.bat

Code: Select all

@echo off
echo aaa|findrepl "aaa" "bbb"|findrepl "bbb" "ccc"

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

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

#104 Post by foxidrive » 26 Jul 2016 21:47

Whatever issue causes this was introduced in Findrepl 2.1
as Findrepl 2.0 works flawlessly in both batch files here.

rem - Nov/20/2014: Version 2 - New switches: /J, /L, /G, /ARG#, and || separate regexp's in /Alternations.


Code: Select all

d:\test>for /L %a in (1,1,10) do @call z-more
ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc


Code: Select all

d:\test>for /L %a in (1,1,10) do @call z
ccc
ccc
ccc
ccc
ccc
ccc
ccc
ccc
ccc
ccc

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

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

#105 Post by foxidrive » 26 Jul 2016 21:50

rem - Dic/15/2014: Version 2.2 - New "data generating " predefined functions for /S switch combined with /J

Code: Select all

d:\test>for /L %a in (1,1,10) do @call z
d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

d:\test\findrepl.bat(784, 4) Microsoft JScript runtime error: Input past end of file

d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

d:\test\findrepl.bat(784, 4) Microsoft JScript runtime error: Input past end of file

d:\test\findrepl.bat(322, 1) Microsoft JScript runtime error: Input past end of file

d:\test\findrepl.bat(784, 4) Microsoft JScript runtime error: Input past end of file

d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

d:\test\findrepl.bat(784, 4) Microsoft JScript runtime error: Input past end of file

d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

d:\test\findrepl.bat(784, 4) Microsoft JScript runtime error: Input past end of file

d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied



Code: Select all

d:\test>for /L %a in (1,1,10) do @call z-more
d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied


ccc

d:\test\findrepl.bat(325, 1) Microsoft JScript runtime error: Permission denied


d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

d:\test\findrepl.bat(325, 1) Microsoft JScript runtime error: Permission denied


ccc

ccc

d:\test\findrepl.bat(318, 1) Microsoft JScript runtime error: Permission denied

ccc

ccc

Post Reply