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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: New regex utility to search and replace strings in files

#16 Post by brinda » 02 Jul 2013 06:52

Antonio,

Thanks again for helping. I am putting the partial file which is used below.

test.txt

Code: Select all

Mainframe ID T03 -412
Check V = 5
Check End = 9

Mainframe ID T04 -412
     Rohm = 1.51
     Pwr = 7.42
     Amp = 0.95
     Cap = - 8.78
     Bpwr = 588.24
     Bchr = 6.358
     Spwr = 90
Check voltage is 415.67

Mainframe ID T05 -412
Power dist : 0
Vdd = 0
Vcal = 0


On the old findrepl using /O:-7:-1(to replace findstr). To convert vertical data to horizontal for ease or comparing this command [findrepl "\r\n" "" ] is used

This gives me a horizontal data from vertical like below.

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 | findrepl "\r\n" ""
     Rohm = 1.51     Pwr = 7.42     Amp = 0.95     Cap = - 8.78     Bpwr = 588.24     Bchr = 6.358     Spwr = 90


on using the command below with the new script, It seems to copy everything

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 /B:"\r\n" ""
Asigno removeCRLF: [\r\n] vs [\n\r]
Replace: procBlocks=true, removeCRLF = false
Mainframe ID T03 -412
Check V = 5
Check End = 9

Mainframe ID T04 -412
Replace + procBlocks + inRange, removeCRLF = false
     Rohm = 1.51
     Pwr = 7.42
     Amp = 0.95
     Cap = - 8.78
     Bpwr = 588.24
     Bchr = 6.358
     Spwr = 90
Check voltage is 415.67

Mainframe ID T05 -412
Power dist : 0
Vdd = 0
Vcal = 00


This method works with the new findrepl but it adds new character

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 | findrepl "\r\n" ""
Asigno removeCRLF: [undefined] vs [\n\r]
     Rohm = 1.51     Pwr = 7.42     Amp = 0.95     Cap = - 8.78     Bpwr = 588.24     Bchr = 6.358     Spwr = 90


thanks again for helping. :)

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

Re: New regex utility to search and replace strings in files

#17 Post by Aacini » 02 Jul 2013 09:33

Oops! I was really tired last night! :shock: It seems that I deleted last program version and posted a previous one with tracing .Echo's. I apologize. :oops: I just posted the good new version above...

I also "forgot" to indicate that you must include the new /VR switch this way:

Code: Select all

for %%g IN (*%%f_LN*) do (
< "%%g" ..\findrepl  "Check Voltage is" /O:-7:-1 /B:"\r\n" "" /VR >> ..\test.htm
)

Perhaps you want to do a couple tests with and without /VR switch and check the differences...

Antonio

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

Re: New regex utility to search and replace strings in files

#18 Post by brinda » 03 Jul 2013 06:32

Antonio,
Thanks and sorry to trouble again :( . Copied the latest script on Page 1 but there is no output on using /VR.

with /VR

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 /B"\r\n" "" /VR

D:\>


without /VR it copies as before

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 /B"\r\n" ""
Mainframe ID T03 -412
Check V = 5
Check End = 9

Mainframe ID T04 -412
     Rohm = 1.51
     Pwr = 7.42
     Amp = 0.95
     Cap = - 8.78
     Bpwr = 588.24
     Bchr = 6.358
     Spwr = 90
Check voltage is 415.67

Mainframe ID T05 -412
Power dist : 0
Vdd = 0
Vcal = 0

D:\>


this command still works as in the first script without comment

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 | findrepl "\r\n" ""
     Rohm = 1.51     Pwr = 7.42     Amp = 0.95     Cap = - 8.78     Bpwr = 588.24     Bchr = 6.358     Spwr = 90


I tested this script on actual logs and it outputs the same as the example. Thanks for looking.

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

Re: New regex utility to search and replace strings in files

#19 Post by Aacini » 03 Jul 2013 07:34

You missed a colon in /B:"\r\n" switch!

Aacini wrote:The colons in all switches are mandatory.


I apologize about this point, but this is the way that "named arguments" works in JScript programs...

Antonio

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

Re: New regex utility to search and replace strings in files

#20 Post by brinda » 03 Jul 2013 07:50

Antonio,
syntax is ok, it replaces grep - that is great. No issue..

my fault. sorry :oops:

I made 2 tries but copied the separate one.

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 /B:"\r\n" ""
Mainframe ID T03 -412
Check V = 5
Check End = 9

Mainframe ID T04 -412
     Rohm = 1.51
     Pwr = 7.42
     Amp = 0.95
     Cap = - 8.78
     Bpwr = 588.24
     Bchr = 6.358
     Spwr = 90
Check voltage is 415.67

Mainframe ID T05 -412
Power dist : 0
Vdd = 0
Vcal = 0

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 /B:"\r\n" "" /VR

D:\>


using win2000 SP4

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

Re: New regex utility to search and replace strings in files

#21 Post by brinda » 03 Jul 2013 08:02

Antonio,

Having a space after B: gives vertical output. just found. posting partial below

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 /B: "\r\n" "" /VR

R
o
h
m

=

1
.
5
1







P
w
r

=

7
.
4
2



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

Re: New regex utility to search and replace strings in files

#22 Post by Aacini » 03 Jul 2013 08:05

Wow! My fault :shock: :cry: :oops:

The line # 153 of the program is this:

Code: Select all

   removeCRLF = (block == "\\n\\r") && (replace == "");

Just interchange the \\n and \\r this way:

Code: Select all

   removeCRLF = (block == "\\r\\n") && (replace == "");


................................

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

Re: New regex utility to search and replace strings in files

#23 Post by brinda » 03 Jul 2013 08:16

Antonio,

Thank you so much for spending your time on this s :D :D :D learnt a lot today

Code: Select all

D:\>< test.txt findrepl "Check voltage is" /O:-7:-1 /B:"\r\n" "" /VR
     Rohm = 1.51     Pwr = 7.42     Amp = 0.95     Cap = - 8.78     Bpwr = 588.2
4     Bchr = 6.358     Spwr = 90


All working. now to go and ammend this wonderful method to the other batch file.

thanks so much.

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

Re: New regex utility to search and replace strings in files

#24 Post by Aacini » 06 Jul 2013 21:25

I changed the /VR switch by just /R. :? I think it is clearer this way (I don't know why I didn't used /R in first place).

Antonio

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

Re: New regex utility to search and replace strings in files

#25 Post by foxidrive » 08 Oct 2013 02:57

I have a query regarding the regular expression syntax

I have a list of words and I want to print the words that contain any combination of a set of letters.

I can do the opposite by this: type wordlist.txt | findrepl /v "[atp]" and words containing a or t or p will be excluded,

If I want to only include words that contain a t e p r s such as pepper then how can I do that?

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

Re: New regex utility to search and replace strings in files

#26 Post by dbenham » 08 Oct 2013 06:44

Assuming you have one word per line, I believe this should work:

Code: Select all

type wordlist.txt | findrepl "^[ateprs]+$"

A similar search should work with FINDSTR as well

Code: Select all

type wordlist.txt | findstr "^[ateprs][ateprs]*$"


Dave Benham

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

Re: New regex utility to search and replace strings in files

#27 Post by foxidrive » 08 Oct 2013 06:53

They work well Dave, ta.

I always find it easier after seeing an example. Both findrepl and findstr work in this example with this regexp too "^[ateprs]*$"

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

Re: New regex utility to search and replace strings in files

#28 Post by dbenham » 08 Oct 2013 10:19

Almost identical, except that regex will also match an empty line. The solutions I posted require at least one valid character.

Dave

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

Re: New regex utility to search and replace strings in files

#29 Post by Squashman » 05 Apr 2014 07:25

Aacini, can you please look into a couple of issues I've noticed?

Issue 1) Findrepl hangs when searching a folder full of files, when it contains various images. Here is an example:

Image from https://github.global.ssl.fastly.net/im ... ws-app.png

code sample follows that demonstrates the issue in Windows 8.1:

Code: Select all

findrepl /i cat <gh-windows-app.png



Issue 2) Findrepl generates a harmless but annoying error if a file to be searched is zero bytes

Thanks

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

Re: New regex utility to search and replace strings in files

#30 Post by Aacini » 06 Apr 2014 09:54

@Squashman,

Well, findrepl is designed to work on text files. Although I don't see any reason because it hangs on a binary file (excepting its size, perhaps), it is necessary to do some tests. It would be easier to filter the files that will be processed...

The same point apply to empty files. I will test these points when I have any spare time.

Antonio

Post Reply