How to count and replace text string in file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to count and replace text string in file?

#1 Post by pstein » 21 Sep 2014 22:31

Assume I want to count the number of occurencies of a certain text string in a file
and replace all these occurencies by another string. How can I do this?

Obviously this is easy with an GUI based editor like Notepad++.
But how can I achieve this from a DOS batchfile?

If necessary a non-dos-batch command but an external tool can be used.

So at the end a script like the following should be available:

set numocc = countocc("D:\work\myfile123.log","mystring")
Echo Number of occurencies = %numocc%
replace("D:\work\myfile123.log","mystring","newstringAAA",all)

Thank you
Peter

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

Re: How to count and replace text string in file?

#2 Post by dbenham » 22 Sep 2014 04:04

This is quite simple if you use my REPL.BAT utility.

Code: Select all

@echo off
setlocal
set "file=D:\work\myfile123.log"
for /f %%N in ('^<"%file%" repl "mystring" "\n$&\n" LX ^| find /c "mystring"') do set numocc=%%N
echo Number of occurrences = %numocc%
type "%file%" | repl "mystring" "newstring" L >"%file%.new"
move /y "%file%.new" "%file%" >nul


Dave Benham
Last edited by dbenham on 26 Sep 2014 10:41, edited 2 times in total.

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

Re: How to count and replace text string in file?

#3 Post by foxidrive » 22 Sep 2014 07:02

Maybe the L switch in repl would be useful, to stop regular expression terms from interfering with the task.
Plus the X switch in the first repl term.

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

Re: How to count and replace text string in file?

#4 Post by dbenham » 22 Sep 2014 07:37

Both good points, thanks. :D
I've edited my prior post.


Dave Benham

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

Re: How to count and replace text string in file?

#5 Post by Squashman » 22 Sep 2014 07:44

We do have the old fashion substitute in the Batch Library. But this won't give you the count of occurrences but could be modified to do so.
http://www.dostips.com/DtCodeCmdLib.php ... substitute

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to count and replace text string in file?

#6 Post by pstein » 26 Sep 2014 10:30

dbenham wrote:

Code: Select all

@echo off
setlocal
set "file=D:\work\myfile123.log"
for /f %%N in ('<"%file%" repl "mystring" "\n$&\n" LX | find /c "mystring"') do set numocc=%%N
echo Number of occurrences = %numocc%
type "%file%" | repl "mystring" "newstring" L >"%file%.new"
move /y "%file%.new" "%file%" >nul



Thank you for this suggestion Dave but when I run this the following error occurs (under 64bit Win7):

"< was unexpected at this time."

I guess a modification is necessary.

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

Re: How to count and replace text string in file?

#7 Post by Squashman » 26 Sep 2014 10:39

I believe you need to escape the PIPE in the FOR arguments.

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

Re: How to count and replace text string in file?

#8 Post by dbenham » 26 Sep 2014 10:42

Yes, that was not my best day answering questions :oops:

I forgot to escape the redirection and the pipe in the command string.

My original post is fixed now.


Dave Benham

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to count and replace text string in file?

#9 Post by pstein » 29 Sep 2014 23:17

Ok, thank you.

However meanwhile a fried told me about the famous "sed" tool as a unix port to windows freely available from here:

http://gnuwin32.sourceforge.net/packages/sed.htm

Since I prefer solutions with short code I give it a try.
For a slight modification of the initial task I want to replace not a string but bytes by other byte(s).

When I code therefore in a DOS batch file:

echo start
sed 's/\xc3\xbc/\xtc/g' <test.txt >testnew.txt
pause

....and run it then I got the following error:

sed: -e expression #1, char 1: unknown command: `''

Why?

Does it matter if the DOS batch file is in ANSI or UTF-8 encoding?

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to count and replace text string in file?

#10 Post by ShadowThief » 30 Sep 2014 01:43

pstein wrote:Ok, thank you.

However meanwhile a fried told me about the famous "sed" tool as a unix port to windows freely available from here:

http://gnuwin32.sourceforge.net/packages/sed.htm

Since I prefer solutions with short code I give it a try.
For a slight modification of the initial task I want to replace not a string but bytes by other byte(s).

When I code therefore in a DOS batch file:

echo start
sed 's/\xc3\xbc/\xtc/g' <test.txt >testnew.txt
pause

....and run it then I got the following error:

sed: -e expression #1, char 1: unknown command: `''

Why?

Does it matter if the DOS batch file is in ANSI or UTF-8 encoding?

Wait, did you use ` instead of '? Because that actually matters. You need to be using '.

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

Re: How to count and replace text string in file?

#11 Post by foxidrive » 30 Sep 2014 01:50

GnuSed under Windows uses double quotes.

Code: Select all

sed "s/\xc3\xbc/\xtc/g" <test.txt >testnew.txt



\xtc
isn't a valid term though...

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to count and replace text string in file?

#12 Post by pstein » 01 Oct 2014 03:44

Ok, thank you.

But how else can I specify a certain byte value as replacement?

How can I specify a german Umlaut (e.g. ü = &uuml;) as replacement?

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

Re: How to count and replace text string in file?

#13 Post by foxidrive » 01 Oct 2014 05:11

pstein wrote:But how else can I specify a certain byte value as replacement?

How can I specify a german Umlaut (e.g. ü = &uuml;) as replacement?



\xtc is specifying a hex byte using \x but tc is not a hex code.

Post Reply