Count two strings in a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Count two strings in a text file

#1 Post by Ken » 02 Feb 2012 07:45

How do I count two strings in a text file.
example text:

Hello Today is a good day

I would like to count all the words 'Hello' and 'day' and pipe it into another text file

example of output
Found the following words - Hello 5 times and day 10 times

Thank you for your help
Ken

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Count two strings in a text file

#2 Post by Ed Dyreen » 02 Feb 2012 11:46

'
This counts occurrences in a single variable, for files it's a little bit more complicated, I don't have time right now :|

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$=Hello Today is a good day,Hello Today is a good day,Hello Today is a good day"
set/ac=0
:count ()
:: (
   if defined $ for %%? in ("!$!") do (
      set "$=!$:*day=!"
      if "!$!" neq "%%~?" set/ac+=1&goto :count
   )
:: )
echo.count=%c%_
pause

Code: Select all

for /?
sorry search.php

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

Re: Count two strings in a text file

#3 Post by Aacini » 02 Feb 2012 21:03

Just one detail: you want not to count strings, but words, that is, strings enclosed in spaces or delimited by comma, semicolon or point, right? Otherwise, the "Today" will be counted as "day"!

The Batch file below correctly works if the file does not include equal-signs:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set Hello=0
set day=0
rem Read and process all the lines in the file
for /F "delims=" %%l in (thefile.txt) do (
   rem Enclose next line between $ delimiters
   set "line=$$%%l$$"
   rem Change word separators ( ,;.) by $ delimiter
   set "line=!line: =$!"
   set "line=!line:,=$!"
   set "line=!line:;=$!"
   set "line=!line:.=$!"
   rem Review each word
   for %%w in (Hello day) do (
      rem Replace the word by a separator
      set "newLine=!line:$%%w$= !"
      rem If the word was in the line...
      if not "!newLine!" == "!line!" (
         rem ... count how many times
         for %%t in (!newLine!) do set /A %%w+=1
         set /A %%w-=1
      )
   )
)
echo Hello %Hello% times and day %day% times

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: Count two strings in a text file

#4 Post by Ken » 03 Feb 2012 14:24

Aacini and Ed Dyreen

Thank you for your help with this code.

Ken

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

Re: Count two strings in a text file

#5 Post by foxidrive » 10 Feb 2012 07:34

Here's another method. It is case insensitive and it will also match words like Hellocopter and daytona

It will also not count this as two occurrences of day "Today is Sunday" but will only count it as 1.

Code: Select all

@echo off
for /f %%a in ('find /c /i "Hello" ^<"file.txt"') do set hello=%%a
for /f %%a in ('find /c /i "day" ^<"file.txt"') do set day=%%a

echo Hello %hello% times and day %day% times


Post Reply