try this:
@ECHO OFF &SETLOCAL
for /f "delims=" %%a in ('^<file find "Starting"') do set "$%%a=7"
for /f %%a in ('set $') do set /a count+=1
echo(%count%
Question
I have a plain text file like below:
11/03/20 09:42:45 APP Service A stopped.
11/03/20 09:42:46 APP Starting service A.
11/03/20 09:42:46 APP Starting service A.
11/03/20 09:44:01 APP Service A stopped.
11/03/20 09:44:02 APP Starting service A.
11/03/20 09:44:02 APP Starting service A.
11/03/20 09:45:02 APP Service A stopped.
...
This file refers only to one service (service A). As you can see, lines that contains the "Starting" substring are duplicated twice each time.
Using a batch file (ms-dos bat file) I want to count the number of lines in the plain text file that contains the substring "Starting" but I do not want to count the duplicate ones.
For example, from below piece of plain text file and ignoring duplicated lines, I want the following output:
2
instead of (taking into account duplicated lines):
4
I know how to do this by taking into account the duplicated lines but not the ignoring them. I am performing below:
findstr /N "Starting" plain_text_file.txt | find /c ":"
Any ideas to not count the duplicated lines?
Maybe, once I have the result (number of lines) I can divide them by 2 but I do not think it is an elegant way to do it.
La solution
try this:
@ECHO OFF &SETLOCAL
for /f "delims=" %%a in ('^<file find "Starting"') do set "$%%a=7"
for /f %%a in ('set $') do set /a count+=1
echo(%count%
Autres conseils
If you can use Cygwin you would use the
uniq
command for achieving this.
You might want to try http://www.richpasco.org/utilities/unique.html to do this in DOS. Syntax is:
UNIQUE <infile.txt >outfile.txt