How to count and replace text string in file?
Moderator: DosItHelp
How to count and replace text string in file?
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
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
Re: How to count and replace text string in file?
This is quite simple if you use my REPL.BAT utility.
Dave Benham
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.
Re: How to count and replace text string in file?
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.
Plus the X switch in the first repl term.
Re: How to count and replace text string in file?
Both good points, thanks.
I've edited my prior post.
Dave Benham

I've edited my prior post.
Dave Benham
Re: How to count and replace text string in file?
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
http://www.dostips.com/DtCodeCmdLib.php ... substitute
Re: How to count and replace text string in file?
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.
Re: How to count and replace text string in file?
I believe you need to escape the PIPE in the FOR arguments.
Re: How to count and replace text string in file?
Yes, that was not my best day answering questions
I forgot to escape the redirection and the pipe in the command string.
My original post is fixed now.
Dave Benham

I forgot to escape the redirection and the pipe in the command string.
My original post is fixed now.
Dave Benham
Re: How to count and replace text string in file?
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?
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?
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: How to count and replace text string in file?
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 '.
Re: How to count and replace text string in file?
GnuSed under Windows uses double quotes.
\xtc isn't a valid term though...
Code: Select all
sed "s/\xc3\xbc/\xtc/g" <test.txt >testnew.txt
\xtc isn't a valid term though...
Re: How to count and replace text string in file?
Ok, thank you.
But how else can I specify a certain byte value as replacement?
How can I specify a german Umlaut (e.g. ü = ü) as replacement?
But how else can I specify a certain byte value as replacement?
How can I specify a german Umlaut (e.g. ü = ü) as replacement?
Re: How to count and replace text string in file?
pstein wrote:But how else can I specify a certain byte value as replacement?
How can I specify a german Umlaut (e.g. ü = ü) as replacement?
\xtc is specifying a hex byte using \x but tc is not a hex code.