user3164140
user3164140

Reputation: 651

How to get the count of a particular word from a file

I have a file called C:\User\abc\Desktop\Mtmlog.txt and its contents are

1 Down@:01:46:44:902 Up@:04:08:00:233

1 Down@:02:40:44:908 Up@:03:08:00:293

1 Down@:05:50:44:978 Up@:06:08:00:263

1 Down@:09:45:44:103 Up@:10:08:00:283

There is an application where the Down and Up time is noted in that file.

To check continuity of application, I want a query to check if the Down count is equal to the Up count.
If the count is equal then it should echo Yes and if the count is different then echo No.

Upvotes: 2

Views: 84

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207455

This is simple and easy to read. It counts the number of lines in which "Up" appears and sets the variable "UPS". Then it counts the number of lines in which "Down" appears and sets the variable "DOWNS".

FOR /F %%C IN ('FIND /C "Up"   ^< yourfile') DO SET UPS=%%C
FOR /F %%C IN ('FIND /C "Down" ^< yourfile') DO SET DOWNS=%%C
if "%UPS%"=="%DOWNS%" echo Yes
if NOT "%UPS%"=="%DOWNS%" echo No

With your input file UPS gets set to 4 and DOWNS gets set to 4, so "Yes" is printed.

Upvotes: 1

foxidrive
foxidrive

Reputation: 41234

This works here:

@echo off
findstr /v "^$" "C:\User\abc\Desktop\Mtmlog.txt" |findstr /v /i /r ".*down.*up.*" >nul && (echo No) || (echo yes)

This is another alternative:

@echo off
set var=0
findstr /v "^$" "C:\User\abc\Desktop\Mtmlog.txt" |findstr /v /i /r ".*down.*up.*" >nul && set var=1
if %var% equ 1 (echo No) else (echo yes)

Upvotes: 1

Related Questions