Count string occurrences in text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Count string occurrences in text file

#31 Post by foxidrive » 31 Jul 2012 11:19

View the source files and see if there is an extra $ in there. Maybe there are two in a row at the end??

kmbarre
Posts: 13
Joined: 30 Jul 2012 07:09

Re: Count string occurrences in text file

#32 Post by kmbarre » 31 Jul 2012 11:25

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.

kmbarre
Posts: 13
Joined: 30 Jul 2012 07:09

Re: Count string occurrences in text file

#33 Post by kmbarre » 31 Jul 2012 11:29

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" . . . . .

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

Re: Count string occurrences in text file

#34 Post by Squashman » 31 Jul 2012 11:30

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?

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

Re: Count string occurrences in text file

#35 Post by foxidrive » 31 Jul 2012 12:15

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

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

Re: Count string occurrences in text file

#36 Post by foxidrive » 31 Jul 2012 12:18

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 &.

kmbarre
Posts: 13
Joined: 30 Jul 2012 07:09

Re: Count string occurrences in text file

#37 Post by kmbarre » 31 Jul 2012 12:20

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.

kmbarre
Posts: 13
Joined: 30 Jul 2012 07:09

Re: Count string occurrences in text file

#38 Post by kmbarre » 31 Jul 2012 12:21

Sorry. Not & but $. My actual search string is not a $ but using it as an example

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

Re: Count string occurrences in text file

#39 Post by foxidrive » 31 Jul 2012 12:24

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.

suren_bin
Posts: 1
Joined: 01 Aug 2012 10:17

Re: Count string occurrences in text file

#40 Post by suren_bin » 01 Aug 2012 10:25

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

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

Re: Count string occurrences in text file

#41 Post by foxidrive » 01 Aug 2012 13:48

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

ellegal
Posts: 1
Joined: 02 Jul 2013 21:28

Re: Count string occurrences in text file

#42 Post by ellegal » 02 Jul 2013 21:46

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.

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

Re: Count string occurrences in text file

#43 Post by foxidrive » 02 Jul 2013 23:45

There is a new tool developed recently that can search in handy ways - see here for findrepl

viewtopic.php?f=3&t=4697

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

Re: Count string occurrences in text file

#44 Post by Aacini » 03 Jul 2013 08:58

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

Post Reply