Page 3 of 3

Re: Count string occurrences in text file

Posted: 31 Jul 2012 11:19
by foxidrive
View the source files and see if there is an extra $ in there. Maybe there are two in a row at the end??

Re: Count string occurrences in text file

Posted: 31 Jul 2012 11:25
by kmbarre
I've done that. :( I opened up the file in ultra edit and did a search -> find -> count for the string. there is one less than what my script is reporting. It may be that this script adds one too many line breaks.

Re: Count string occurrences in text file

Posted: 31 Jul 2012 11:29
by kmbarre
I'm not familar with SED so I'm looking at their site for some more insight on the switches. I'm curious as to the \ before the search string
eg. sed "s/\&/n\/g" . . . . .

Re: Count string occurrences in text file

Posted: 31 Jul 2012 11:30
by Squashman
kmbarre wrote:I've done that. :( I opened up the file in ultra edit and did a search -> find -> count for the string. there is one less than what my script is reporting. It may be that this script adds one too many line breaks.

You sure there isn't an existing Line Break in the file already. LF or CR?

Re: Count string occurrences in text file

Posted: 31 Jul 2012 12:15
by foxidrive
If it is consistant with your files then just subtract 1 from the total

Here's a single batch file to do *.txt

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%a in (*.txt) do (
sed "s/\$/\n/g" "%%a" >file.tmp
for /f %%b in ('find /c /v "" ^<file.tmp') do set /a num=%%b-1
echo %%a  - !num!
del file.tmp
)
pause

Re: Count string occurrences in text file

Posted: 31 Jul 2012 12:18
by foxidrive
kmbarre wrote:I'm not familar with SED so I'm looking at their site for some more insight on the switches. I'm curious as to the \ before the search string
eg. sed "s/\&/n\/g" . . . . .


It is escaping the $ but you changed it to &.

Re: Count string occurrences in text file

Posted: 31 Jul 2012 12:20
by kmbarre
nope. no line breaks. I've looked at he files with a hex editor and found nothing. For all files, regardless of size, they contain only one line. no CR or LF.

Re: Count string occurrences in text file

Posted: 31 Jul 2012 12:21
by kmbarre
Sorry. Not & but $. My actual search string is not a $ but using it as an example

Re: Count string occurrences in text file

Posted: 31 Jul 2012 12:24
by foxidrive
kmbarre wrote:Sorry. Not & but $. My actual search string is not a $ but using it as an example



if you don't give correct details then you can often get broken code. The escape is not necessary in many cases.

Re: Count string occurrences in text file

Posted: 01 Aug 2012 10:25
by suren_bin
Hi, I tried to create a code to find a string "MK" in series of files in a folder. The output file should list file name and count of string "MK". Somehow countstr does not get couted in the code below. Please help!

@echo off
set pathname=Z:\CAD\PDMS\Project\HIBFLR\BTHINFO\MAR
set outpath=%pathname%\1.prn
if exist %outpath% del %outpath%
set "str=MK"

rem cd %pathname%
rem ECHO %pathname%
Z:
set pathletter=%CD:~0,2%
rem -----------------------
ECHO Path Drive Changed to %pathletter%

FOR %%j IN (%pathname%\*.txt ) DO (

set cnt=0
for /f ^"eol^=^

delims^=^" %%a in ('"findstr /i "/c:%str%" %%j"') do set "ln=%%a"&call :countStr
echo Spool for %%j is %cnt%
echo Spool for %%j is %cnt% >>%outpath%
)
pause
exit /b
set cnt=0

:countStr
setlocal enableDelayedExpansion
:loop
if defined ln (
set "ln2=!ln:*%str%=!"
if "!ln2!" neq "!ln!" (
set "ln=!ln2!"
set /a "cnt+=1"
goto :loop
)
)
endlocal & set cnt=%cnt%
exit /b

Re: Count string occurrences in text file

Posted: 01 Aug 2012 13:48
by foxidrive
You could also use the technique using GnuSED.

Your issue can be illustrated here:

Code: Select all

@echo off
(
for %%a in (one) do call :next %%a
echo ...[%var%]...
)
pause
goto :EOF
:next
set var=two


and this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
(
for %%a in (one) do call :next %%a
echo ...[!var!]...
)
pause
goto :EOF
:next
set var=two

Re: Count string occurrences in text file

Posted: 02 Jul 2013 21:46
by ellegal
The example script (dbenham) does not handle "<" being in the input file. I'm searching an xml document am am getting the error "< was unexpected at this time". What changes need to be made to your example script for this case? I executed a find/replace for "<" to test that this is the culprit.

Re: Count string occurrences in text file

Posted: 02 Jul 2013 23:45
by foxidrive
There is a new tool developed recently that can search in handy ways - see here for findrepl

viewtopic.php?f=3&t=4697

Re: Count string occurrences in text file

Posted: 03 Jul 2013 08:58
by Aacini
For example, using FindRepl.bat:

Code: Select all

> < test.txt FindRepl.bat /N
1:hand randomtext<hand randomtext
2:hand <randomtext
3:hand random<text
4:hand randomtext

> < test.txt FindRepl.bat "hand" /$:0 /N
1: "hand" "hand"
2: "hand"
3: "hand"
4: "hand"

> echo %errorlevel%
5

> < test.txt FindRepl.bat "<" /$:0 /N
1: "<"
2: "<"
3: "<"

> echo %errorlevel%
3


Antonio