1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

count a string in a file

Discussion in 'T&T - Scripting' started by JB2, Jan 28, 2015.

  1. JB2

    JB2

    Joined:
    Jan 9, 2015
    Messages:
    9
    Likes Received:
    0
    what would the best way to count how many times a string appears in txt file say "windows xp" and "windows 7" ?
     
  2. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    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
     
  3. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
  4. JB2

    JB2

    Joined:
    Jan 9, 2015
    Messages:
    9
    Likes Received:
    0
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,955
    Likes Received:
    30
    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
     
  6. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    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
     
  7. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    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
     
  8. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    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
     
  9. dcantor

    Joined:
    May 29, 2008
    Messages:
    507
    Likes Received:
    3
    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
     
  10. Rick Reinckens

    Joined:
    Oct 18, 2009
    Messages:
    96
    Likes Received:
    4
    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.)
     
  11. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,955
    Likes Received:
    30
    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
     

Share This Page