Batch count occurrences of a string in file

April 13, 2011 at 13:25:05
Specs: Windows 7
I have an input file f1.txt in which there are some strings(each string on a different row). The second file f2.txt contains some text. I must print the number of occurrences of each string from f1.txt in f2.txt.

Example:
f1.txt:

a
b
cal


f2.txt:

andrew alex bianca
andreea calin
edward beth


the output should be:

the number of occurrences of a is 8
the number of occurrences of b is 2
the number of occurrences of cal is 1


I tried to solve it with findstr but it returns the number of lines on which the strings appear, not the number of occurrences.

Thanks,
Vanessa


See More: Batch count occurrences of a string in file

Report •


#1
April 13, 2011 at 17:21:35
You have to parse each line.

Report •

#2
April 17, 2011 at 00:55:23
I tried to do this but it doesn't work:

for /f %%e in (f1.txt) do (
set count=0
for /f "delims=" %%a in ('type "%1" ^| findstr "[^a-zA-Z]%%e[^a-zA-Z]"') do (
set /a count +=1
)
echo variable %%e appears !count!
)


%1 is f2.txt, becouse I've read it from the standard input....I also used the regex [^a-zA-Z]%%e[^a-zA-Z] near the string I am looking for becouse there must not be any letters around %%e...however, I might have characters like <>)(& near %%e


Report •

#3
August 22, 2011 at 02:33:22
Hi
Do you still search for a solution for it?

Report •
Related Solutions


Ask Question