Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

count a string in a file

Jan
9
0
what would the best way to count how many times a string appears in txt file say "windows xp" and "windows 7" ?
 
The TPIPE command is a solution, using the grep option,
Code:
/grep=Type,IncludeLineNumbers,IncludeFilename,MatchCase,CountMatches,PatternType,UTF8,IgnoreEmpty,Pattern

You would set CountMatches to 1 to output a count of the number of matches.

Do a search of the forum for TPIPE to find examples.

Joe
 
That doesn't seem to count more than one match per line.
Code:
v:\> echo foofoo | tpipe /grep=3,0,0,0,1,0,0,0,foo
1

v:\> echo foofoo^r^nfoo | tpipe /grep=3,0,0,0,1,0,0,0,foo
2

I suppose you could first replace each occurrence of the string with the string followed by a newline (there should be something more elegant).
Code:
v:\> echo foofoo^r^nfoo | tpipe /replace=4,0,0,0,0,0,0,0,0,foo,foo\r\n /grep=3,0,0,0,0,0,0,0,foo
foo
foo
foo

v:\> echo foofoo^r^nfoo | tpipe /replace=4,0,0,0,0,0,0,0,0,foo,foo\r\n /grep=3,0,0,0,1,0,0,0,foo
3
 
Just remembered the FIND command;
Code:
type problems.txt
AsusACPIServer
AsusEPCMonitor
AsusTray
ETDWare
ETDWareDetect

ffind /l/v/t"Asus" problems.txt

---- C:\utils\problems.txt
[1] AsusACPIServer
[2] AsusEPCMonitor
[3] AsusTray

  3 lines in  1 file

Joe
 
Hmmm... if there are two occurrences of the same string in a line, FIND only counts them once.

If I modify my file, thus;
Code:
AsusACPIServer  AsusACPIServer
AsusEPCMonitor
AsusTray
ETDWare
ETDWareDetect

...and then load my file into VIEW, press Ctrl-F, type in Asus then click Count All it returns 4, which is correct.

Joe
 
Here is a user-defined variable function that counts the number of occurrences of a string in a file;
Code:
StringCount=%@script[vbscript,Const ForReading = 1::set fso=CreateObject("Scripting.FileSystemObject"):set oFile = FSO.OpenTextFile("%1", ForReading):set stdout=fso.GetStandardStream(1):strContents = oFile.ReadAll:oFile.Close::Set rgxp = New Regexp:rgxp.Pattern = "%2":rgxp.IgnoreCase = False:rgxp.Global = True:Set matches = rgxp.Execute( strContents )::stdout.WriteLine matches.Count:]

Here is the VBScript in a more readable form;
Code:
Const ForReading = 1

set fso=CreateObject("Scripting.FileSystemObject")
set oFile = FSO.OpenTextFile("%1", ForReading)
set stdout=fso.GetStandardStream(1)
strContents = oFile.ReadAll
oFile.Close

Set rgxp = New Regexp
rgxp.Pattern = "%2"
rgxp.IgnoreCase = False
rgxp.Global = True
Set matches = rgxp.Execute( strContents )

stdout.WriteLine matches.Count

Now, when I do
Code:
echo %@StringCount[problems.txt,Asus]
I get back
Code:
4
Joe
 
If you only want the count, FFIND can be used by specifying /X, thus:
Code:
~\work> type x.txt                                                                                       [49]  0:00:00.020
C:\fixups\add_enlarge_picture_opti.user.js
C:\fixups\allow_fixups.user.js
C:\fixups\better_epguides.user.js

~\work> ffind /l /v /t"fixups" x.txt

---- C:\Users\DCantor\work\x.txt
[1] C:\fixups\add_enlarge_picture_opti.user.js
[2] C:\fixups\allow_fixups.user.js
[3] C:\fixups\better_epguides.user.js

  3 lines in      1 file

~\work> ffind /l /v /t"fixups" /x x.txt

---- C:\Users\DCantor\work\x.txt
Offset: 3  (3h)
Offset: 47  (2fh)
Offset: 60  (3ch)
Offset: 79  (4fh)

  4 lines in      1 file

~\work> echo %_ffind_matches
4
 
If you only want the count, FFIND can be used by specifying /X, thus:
I'm trying the ffind command and I'm finding (no pun intended ...) that it automatically cuts off at 6 matches in one file. I'm trying to check a large file for > < and % and I've added a bunch of each. When I run ffind, for each character it only shows 6 lines. I deliberately added /-50 which means "stop counting after 50 occurrences" but it still stops after 6.

(Just to be clear: I'm running one search for >, a second search for %, and a third search for <. I did find that I can search for > or < with /t">" but for % I have to use /t"^%" That's not specified in the Help.)
 
I'm trying the ffind command and I'm finding (no pun intended ...) that it automatically cuts off at 6 matches in one file. I'm trying to check a large file for > < and % and I've added a bunch of each. When I run ffind, for each character it only shows 6 lines. I deliberately added /-50 which means "stop counting after 50 occurrences" but it still stops after 6.

(Just to be clear: I'm running one search for >, a second search for %, and a third search for <. I did find that I can search for > or < with /t">" but for % I have to use /t"^%" That's not specified in the Help.)
Seems OK here, though (far below) "24 lines" is misleading. It was actually 24 occurrences of "%" (in 7 lines).
Code:
v:\> ffind /t"^%" /v /x avtemp.btm

---- V:\avtemp.btm
Offset: 198  (c6h)
Offset: 204  (cch)
Offset: 578  (242h)
Offset: 587  (24bh)
Offset: 595  (253h)
Offset: 604  (25ch)
Offset: 617  (269h)
Offset: 626  (272h)
Offset: 633  (279h)
Offset: 642  (282h)
Offset: 661  (295h)
Offset: 668  (29ch)
Offset: 677  (2a5h)
Offset: 701  (2bdh)
Offset: 707  (2c3h)
Offset: 748  (2ech)
Offset: 777  (309h)
Offset: 784  (310h)
Offset: 790  (316h)
Offset: 800  (320h)
Offset: 817  (331h)
Offset: 824  (338h)
Offset: 852  (354h)
Offset: 859  (35bh)

  24 lines in  1 file

v:\> echo %_ffind_matches
24
 

Similar threads

Back
Top