1

Using below I was able to count total number of occurrences of a single word and getting result as given below.

@echo off
set "word=Windows"
set file=log.txt
set cnt=0
for /f ^"eol^=^

delims^=^" %%a in ('"findstr /i "/c:%word%" %file%"') do set "ln=%%a"&call :countWord

echo Server_Type   Total_Users    >result.txt
echo %word%           %cnt%       >>result.txt
exit /b

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

result.txt

Server_Type  Total_Users
Windows          24

now i want to add 6 new words like Linux, MacOS, Andriod, Unix....etc to search for in the same log file and get result in same format.

but not getting how to achieve that using FINDSTR and Is that possible given the limited RegExp capability of Findstr ? any suggestion please ?

Sunny
  • 7,812
  • 10
  • 34
  • 48

3 Answers3

1

I've haven't tried this, but the concept would be such as:

@echo off
set "word=Windows Linux MacOS ..."
set file=log.txt
set cnt=0
for %%i in (%word%) do (
for /f ^"eol^=^
...
<the rest of your code>
...
)
l'L'l
  • 44,951
  • 10
  • 95
  • 146
1

I slightly modified your program in order to convert cnt variable into an array that have the different words as subscripts, for example cnt[Windows]=0, cnt[Linux]=0, etc. so :countWords subroutine search all words in each matching line. I also eliminated the setlocal from :countWords subroutine in order to return the values of cnt array in an easier way.

@echo off
setlocal EnableDelayedExpansion

set "words=Windows Linux MacOS Andriod Unix"
set file=log.txt
for %%a in (%words%) do set cnt[%%a]=0
for /f ^"eol^=^

delims^=^" %%a in ('"findstr /i "%words%" %file%"') do call :countWords "%%a"


(echo Server_Type   Total_Users    
for %%a in (%words%) do (
   echo %%a           !cnt[%%a]!       
)) > result.txt
exit /b

:countWords
set wordList=%words%
:nextWord
   for /F "tokens=1*" %%a in ("%wordList%") do (
      set word=%%a
      set wordList=%%b
   )
   set "ln=%~1"
   :loop
   if defined ln (
      set "ln2=!ln:*%word%=!"
      if "!ln2!" neq "!ln!" (
         set "ln=!ln2!"
         set /a "cnt[%word%]+=1"
         goto :loop
      )
   )
if defined wordList goto nextWord
exit /b
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Hi Aacini i have tested your code. If you have a "Unix_foo" or "Windowstoto" it count them to. – SachaDee Dec 14 '13 at 01:23
  • @sachadee..actually i wanted to count words Unix, Windows..without matter how they come in log...as you also would be agree that `Unix_foo` or `Windowstoto`are displaying occurrence of words `Unix` and `Windows` having no reason to ignore them..:) – Sunny Dec 14 '13 at 03:49
0

An easier way to get the counts : you just put a # before the name of the searching string and increase the value of this new Variable (if the string is really the same) with Set /a #%%a+=1 and then with a set # you get all your counts. Very simple and efficient. (sorry for my terrible english)

@ECHO OFF

set "$file=log.txt"
set "$Lsearch=windows unix linux macOs Android"


for %%a in (%$LSearch%) do (set #%%a=0
                  for /f "delims=" %%* in ('type %$file% ^| findstr /i "%%a"') do if /i "%%*"=="%%a" set /a #%%a+=1)

for /f "tokens=1,2 delims==" %%a in ('set #') do echo Server : %%a Total User : %%b
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • 1
    1. This program counts searched words only when they appear alone in a line; 2. It would count _lines_ with strings, not _individual strings_ as the OP requires (that is the reason because the program is so complicated); 3. It execute `findstr` command as many times as target words (it is faster to execute it just once, specially if the file is large). PS - Why you use strange characters, like `$` in variable names or `%%*` in FOR parameter? Batch files are enough difficult to read without these "aids"! `8-)` – Aacini Dec 14 '13 at 03:28
  • thanks for your answer @sachadee...but for a log file even with a single word `windows`, its not giving the correct result. – Sunny Dec 14 '13 at 03:40
  • To have all the occurence, even the `windowsSomething` you just have to delete this : `if /i "%%*"=="%%a"` and then it will count every occurence. – SachaDee Dec 14 '13 at 11:02
  • please check it for a log file having `windows windows` in same line...its ignoring the 2nd windows word. – Sunny Dec 14 '13 at 12:41