Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: how to get the Count of string in file  (Read 37071 times)

0 Members and 1 Guest are viewing this topic.

victoria



    Beginner

    Thanked: 1
    Re: How to get the Count of string in file
    « Reply #30 on: August 12, 2010, 11:38:02 AM »
    We know it\'s you, Bill?

    What is Salmon Trout talking about?

    Is Salmon Trout part of the ComputerHope.com Staff?
    Have a Nice Day

    Salmon Trout

    • Guest
    Re: how to get the Count of string in file
    « Reply #31 on: August 12, 2010, 11:42:07 AM »
    That third person thing is a "dead giveaway" as Londoners say, Bill.

    victoria



      Beginner

      Thanked: 1
      Re: how to get the Count of string in file
      « Reply #32 on: August 12, 2010, 11:50:49 AM »
      arun*America*MSC~INS*dfffs*Sdfsd*sdfsd~ssfsd*sdfsd~INS*dfffs*sdfsdf*sdfs~

      I need to get a count of INS* in the above file. Am new to DOS Commands.


      Arunavlp,

      Im sorry the thread got off topic.

      Swindwinder* and Ghostdog  provided excellent methods for counting the number of times a string appears in a document.

      Please ignore the off topic posts.

      Good Luck

      *   Reply #6 on: August 05, 2010, 04:31:42 AM
      « Last Edit: August 12, 2010, 12:04:13 PM by victoria »
      Have a Nice Day

      Salmon Trout

      • Guest
      Re: how to get the Count of string in file
      « Reply #33 on: August 12, 2010, 12:31:31 PM »
      Victoria, if you aren't Billrich, how come you have access to his Photobucket account?

      victoria



        Beginner

        Thanked: 1
        Re: how to get the Count of string in file
        « Reply #34 on: August 12, 2010, 02:37:11 PM »

        Thanks It works..  but please let me know if we can do it in Find Command....

        Arun S.

        Arun,

        Im sorry Arun the off topic posts continue.

        Some posters never make any suggestions for counting strings in a document.

        These posters write about topics completely unrelated to counting strings.

        _____________________________

        sed s/22/22*n/g yr2010.doc   | egrep -c 22
        Number of 22 strings is 12 in calendar.  One 22 string for each month

        * use a blackslash above to add a newline for strings without a newline


        Have a Nice Day

        vishuvishal



          Beginner
        • Thanked: 3
          Re: how to get the Count of string in file
          « Reply #35 on: August 12, 2010, 03:12:50 PM »
          Can we use FINDSTR

          vishuvishal



            Beginner
          • Thanked: 3
            Re: how to get the Count of string in file
            « Reply #36 on: August 12, 2010, 03:18:44 PM »
            I have an idea that we search for the end of file
            till then run this command in loop.
            Run a variable counter. Till the loop runs.
            Once we reach to End of file stop the loop.
            Check the variable. That would of number of strings.


            and we can also use

            for /f "delims=" %%i in (id.txt)  do (
            echo i = %%i
            )

            if we can apply all these.
            Can anyone create this.

            That would be done.

            Thanks and regards
            vishu

            Salmon Trout

            • Guest
            Re: how to get the Count of string in file
            « Reply #37 on: August 12, 2010, 03:25:59 PM »

            These posters write about topics completely unrelated to counting strings.


            Like your breach of the forum rules. Squirm how you like, you've been rumbled!


            victoria



              Beginner

              Thanked: 1
              Re: how to get the Count of string in file
              « Reply #38 on: August 12, 2010, 03:26:37 PM »
              Can we use FINDSTR

              Let us see your code.

              Findstr with a counter might work?  Findstr will usually only print the line where the string is found.  When the string appears twice in the same line, only one string is counted.  Therefore the final count is wrong.  

              I will try findstr again.  Sidewinder and the other experts stated findstr will not work.

              Let us see your code.
              Have a Nice Day

              victoria



                Beginner

                Thanked: 1
                Re: how to get the Count of string in file
                « Reply #39 on: August 12, 2010, 04:45:15 PM »

                for /f *delims=* %%i in (id.txt)  do (
                echo i = %%i
                )

                vishu


                C:test>type vis812.bat
                REM  Replace * with a double quote symbol
                @echo off
                set /a  c=0
                setlocal enabledelayedexpansion
                for /f *tokens=1-5* %%i in (id.txt)  do (
                echo %%i %%j %%k
                if *%%i*==*the* set /a c=!c! + 1
                if *%%j*==*the* set /a c=!c! + 1
                if *%%k*==*the* set /a c=!c! + 1
                )
                echo count=%c%
                echo.
                echo Display id.txt
                echo.
                type id.txt

                Output.

                C:test>vis812.bat
                the
                the
                the
                the
                the the the
                the the the
                count=10

                Display id.txt

                the
                the
                the
                the
                the the the
                the the the

                p.s. The above code uses only batch code but not findstr.
                The code will only count the string in the id.txt. There are 10 theS in the id.txt.
                The code will most likely not work with other text files
                Have a Nice Day

                victoria



                  Beginner

                  Thanked: 1
                  Re: how to get the Count of string in file
                  « Reply #40 on: August 12, 2010, 05:50:23 PM »
                  Can we use FINDSTR


                  C:test>findstr  22  yr2010.doc
                  17 18 19 20 21 22 23   21 22 23 24 25 26 27   21 22 23 24 25 26 27
                  18 19 20 21 22 23 24   16 17 18 19 20 21 22   20 21 22 23 24 25 26
                  18 19 20 21 22 23 24   22 23 24 25 26 27 28   19 20 21 22 23 24 25
                  17 18 19 20 21 22 23   21 22 23 24 25 26 27   19 20 21 22 23 24 25

                  C:test>findstr  22  yr2010.doc | find /c /v **
                  4

                  C:test>

                  Vis,

                  Even though each line above  has three 22s ; only one is counted by findstr.

                  I do not know  to modify so findstr counts all strings.

                  **  use the double quote symbol above
                  Have a Nice Day

                  victoria



                    Beginner

                    Thanked: 1
                    How to get the Count of string in file
                    « Reply #41 on: August 12, 2010, 06:38:11 PM »
                    Can we use FINDSTR

                    REM  Replace * with double quote
                    C:test>type   yr812.bat
                    @echo off
                    set /a  c=0
                    setlocal enabledelayedexpansion
                    for /f *tokens=1-26* %%a in (yr2010.doc)  do (

                    if *%%a*==*22* set /a c=!c! + 1
                    if *%%b*==*22* set /a c=!c! + 1
                    if *%%c*==*22* set /a c=!c! + 1
                    if *%%d*==*22* set /a c=!c! + 1
                    if *%%e*==*22* set /a c=!c! + 1
                    if *%%f*==*22* set /a c=!c! + 1
                    if *%%g*==*22* set /a c=!c! + 1
                    if *%%h*==*22* set /a c=!c! + 1
                    if *%%i*==*22* set /a c=!c! + 1
                    if *%%j*==*22* set /a c=!c! + 1
                    if *%%k*==*22* set /a c=!c! + 1
                    if *%%l*==*22* set /a c=!c! + 1
                    if *%%m*==*22* set /a c=!c! + 1
                    if *%%n*==*22* set /a c=!c! + 1
                    if *%%0*==*22* set /a c=!c! + 1
                    if *%%p*==*22* set /a c=!c! + 1
                    if *%%q*==*22* set /a c=!c! + 1
                    if *%%r*==*22* set /a c=!c! + 1
                    if *%%s*==*22* set /a c=!c! + 1
                    if *%%t*==*22* set /a c=!c! + 1
                    if *%%u*==*22* set /a c=!c! + 1
                    if *%%v*==*22* set /a c=!c! + 1
                    if *%%w*==*22* set /a c=!c! + 1
                    if *%%x*==*22* set /a c=!c! + 1
                    if *%%y*==*22* set /a c=!c! + 1
                    if *%%z*==*22* set /a c=!c! + 1
                    )
                    echo count=%c%

                    echo.
                    echo Display yr2010.doc
                    echo.

                    Output:

                    C:test>yr812.bat
                    count=12

                    Display yr2010.doc

                    C:test>
                    Have a Nice Day

                    victoria



                      Beginner

                      Thanked: 1
                      Re: how to get the Count of string in file
                      « Reply #42 on: August 12, 2010, 07:23:00 PM »
                      Can we use FINDSTR

                      Vis,
                      ( Code has not been fully tested but my price is right.)
                      REM This generic batch string counter should work for most files and strings
                      Rem replace * with double quote symbol
                      Rem Usage:  cnt812.bat string  file.txt
                      REM Usage:  cnt812.bat  the  id.txt
                      C:test>type cnt812.bat
                      @echo off
                      set /a  c=0
                      setlocal enabledelayedexpansion
                      for /f *tokens=1-26* %%a in (%2)  do (

                      if *%%a*==*%1* set /a c=!c! + 1
                      if *%%b*==*%1* set /a c=!c! + 1
                      if *%%c*==*%1* set /a c=!c! + 1
                      if *%%d*==*%1* set /a c=!c! + 1
                      if *%%e*==*%1* set /a c=!c! + 1
                      if *%%f*==*%1* set /a c=!c! + 1
                      if *%%g*==*%1* set /a c=!c! + 1
                      if *%%h*==*%1* set /a c=!c! + 1
                      if *%%i*==*%1* set /a c=!c! + 1
                      if *%%j*==*%1* set /a c=!c! + 1
                      if *%%k*==*%1* set /a c=!c! + 1
                      if *%%l*==*%1* set /a c=!c! + 1
                      if *%%m*==*%1* set /a c=!c! + 1
                      if *%%n*==*%1* set /a c=!c! + 1
                      if *%%0*==*%1* set /a c=!c! + 1
                      if *%%p*==*%1* set /a c=!c! + 1
                      if *%%q*==*%1* set /a c=!c! + 1
                      if *%%r*==*%1* set /a c=!c! + 1
                      if *%%s*==*%1* set /a c=!c! + 1
                      if *%%t*==*%1* set /a c=!c! + 1
                      if *%%u*==*%1* set /a c=!c! + 1
                      if *%%v*==*%1* set /a c=!c! + 1
                      if *%%w*==*%1* set /a c=!c! + 1
                      if *%%x*==*%1* set /a c=!c! + 1
                      if *%%y*==*%1* set /a c=!c! + 1
                      if *%%z*==*%1* set /a c=!c! + 1
                      )
                      echo count=%c%

                      echo.
                      echo Display %2
                      echo.
                      type %2

                      Output:

                      C:test>cnt812.bat  the  id.txt
                      count=10

                      Display id.txt

                      the
                      the
                      the
                      the
                      the the the
                      the the the

                      C:test>
                      Have a Nice Day

                      victoria



                        Beginner

                        Thanked: 1
                        Re: how to get the Count of string in file
                        « Reply #43 on: August 12, 2010, 08:57:20 PM »
                        Can we use FINDSTR


                        sed s/the/the*n/g id.txt  |  findstr the   | find /c /v **
                        count=10

                        * replace * with backslash symbol
                        **  replace ** with two double quotes

                        sed for windows is an easy download

                        sed  means stream editor
                        Have a Nice Day

                        Salmon Trout

                        • Guest