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 35547 times)

0 Members and 1 Guest are viewing this topic.

arunavlp

    Topic Starter


    Greenhorn

    how to get the Count of string in file
    « on: August 03, 2010, 04:43:17 AM »
    hi,

    Am having a file with 1 line having a file size of 35MB.


    Eg:-
    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.

    Please help me.

    Thanks in Advance.

    Regards,
    Arun S.

    ghostdog74



      Specialist

      Thanked: 27
      Re: how to get the Count of string in file
      « Reply #1 on: August 03, 2010, 08:19:16 AM »
      hi,

      Am having a file with 1 line having a file size of 35MB.


      Eg:-
      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.

      Please help me.

      Thanks in Advance.

      Regards,
      Arun S.

      download  gawk for windows,
      then
      Code: [Select]
      c:\test> gawk "{m=gsub("INS",""); total+=m}END{print "total:" total}" file

      arunavlp

        Topic Starter


        Greenhorn

        Re: how to get the Count of string in file
        « Reply #2 on: August 04, 2010, 11:21:59 PM »
        hi ,

        Thanks for suggestion. but i got an error message like this

        30.834
        gawk: {m=gsub(INS,");
        gawk:             ^ unterminated string

        i dont know wht this error means. Please help me on this.


        Regards,
        Arun S.

        ghostdog74



          Specialist

          Thanked: 27
          Re: how to get the Count of string in file
          « Reply #3 on: August 04, 2010, 11:57:50 PM »
          Escape your double quotes

          Code: [Select]
          c:\test> gawk "{m=gsub(\"INS\",\"\"); total+=m}END{print \"total:\" total}" file

          arunavlp

            Topic Starter


            Greenhorn

            Re: how to get the Count of string in file
            « Reply #4 on: August 05, 2010, 12:34:57 AM »
            hi,

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

            Regards,
            Arun S.

            ghostdog74



              Specialist

              Thanked: 27
              Re: how to get the Count of string in file
              « Reply #5 on: August 05, 2010, 01:14:50 AM »
              hi,

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

              Regards,
              Arun S.
              i personally wouldn't bother. find (or findstr) just find the string on a line for you. It won't count how many there are. More involved programming is needed. ( that i will leave it someone else who has the expertise and time to show you, )
              When parsing files and doing string manipulation, use a good tool for the job.

              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: how to get the Count of string in file
              « Reply #6 on: August 05, 2010, 04:31:42 AM »
              The find command will count the lines with the search argument. If a line has more than one occurrence of the search argument, it still counts for one. Findstr does not do counting but allows for multiple search arguments and a limited form of regular expressions.

              You can use VBScript which came with your Windows machine. The little demo script will prompt the user for the file name and the search argument. It can be tweaked to remove the prompts (which will probably gut the majority of the script). ;D

              Code: [Select]
              Const ForReading = 1

              Set fso = CreateObject("Scripting.FileSystemObject")

              Do
                WScript.StdOut.Write "Please enter file name: "
                strFile = WScript.StdIn.ReadLine
                If fso.FileExists(strFile) Then
                Set objFile = fso.OpenTextFile(strFile, ForReading)
              strCharacters = objFile.ReadAll
                Exit Do
                Else
                  WScript.StdOut.Write "Invalid file name ... Try Again" & vbCrLf
                End If
              Loop

              Do
                WScript.StdOut.Write "Please enter character string: "
                strToCount = WScript.StdIn.ReadLine
                If strToCount <> "" Then Exit Do
              Loop

              strTemp = Replace(LCase(strCharacters), LCase(strToCount), "")
              WScript.Echo "Occurences of:", strToCount, "=", (Len(strCharacters) - Len(strTemp)) / Len(strToCount)

              objFile.Close

              Save the script with a vbs extension and run only from the command prompt as: cscript scriptname.vbs

              Good luck.  ;D
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              vishuvishal



                Beginner
              • Thanked: 3
                Re: how to get the Count of string in file
                « Reply #7 on: August 06, 2010, 06:38:36 PM »
                I can give you Idea what it should like to be:

                set /p pass= <string.txt
                echo %pass%
                call set new=%%pass:~%a%,1%%
                set /a a=%a% + 1
                  set key=%key%%new%
                echo %new%

                This new will give you the number of string.
                However, I am going will give you further details tommorrow

                Thanks and regard
                vishu

                vishuvishal



                  Beginner
                • Thanked: 3
                  Re: how to get the Count of string in file
                  « Reply #8 on: August 07, 2010, 02:33:12 AM »
                  Code: [Select]
                  set /p pass=<string.txt
                  echo %pass%
                  :st
                  call set new=%%pass:~%a%,1%%
                  echo a=%a% + 1
                  echo %a%
                  set key=%key%%new%
                  echo %new%
                  echo %key%
                  pause
                  ::if %new% ==; goto :EOF

                  pause
                  goto :st


                  All we need to fix is loop.
                  Change the string.txt to your file drive:path\file name
                  Gave you a best option


                  victoria



                    Beginner

                    Thanked: 1
                    Re: how to get the Count of string in file
                    « Reply #9 on: August 07, 2010, 01:04:33 PM »
                    @echo  off

                    sed s/the/the\\n/g yz.txt | egrep -c the



                    counthe.bat
                    10
                    type yz.txt
                    the
                    the
                    the
                    the
                    the the the
                    the the the
                    « Last Edit: August 07, 2010, 01:18:26 PM by victoria »
                    Have a Nice Day

                    victoria



                      Beginner

                      Thanked: 1
                      Re: how to get the Count of string in file
                      « Reply #10 on: August 07, 2010, 02:21:14 PM »
                      Two \\ should be one

                      C:\\test>type   cntstr.bat
                      rem @echo  off
                      sed s/%1/%1\\n/g %2 | egrep -c %1

                      C:\\test>cntstr.bat  the yz.txt

                      C:\\test>rem @echo  off

                      C:\\test>sed s/the/the\\n/g yz.txt   | egrep -c the
                      10

                      C:\\test>type yz.txt
                      the
                      the
                      the
                      the
                      the the the
                      the the the
                      Have a Nice Day

                      victoria



                        Beginner

                        Thanked: 1
                        Re: how to get the Count of string in file
                        « Reply #11 on: August 07, 2010, 05:42:49 PM »
                        Only one \\ backslash each time

                        type cntstr.bat
                        rem @echo  off
                        sed s/%1/%1\\n/g %2 | egrep -c %1

                        cntstr.bat   22  yr2010.doc

                        rem @echo  off

                        sed s/22/22\\n/g yr2010.doc   | egrep -c 22
                        12

                        « Last Edit: August 07, 2010, 06:02:46 PM by victoria »
                        Have a Nice Day

                        victoria



                          Beginner

                          Thanked: 1
                          Re: how to get the Count of string in file
                          « Reply #12 on: August 08, 2010, 03:47:04 PM »
                          Have a Nice Day

                          victoria



                            Beginner

                            Thanked: 1
                            How to get the Count of string in file
                            « Reply #13 on: August 08, 2010, 04:05:30 PM »
                            Output for reply #6 by sidewinder


                            cscript   swcnt.vbs
                            Microsoft (R) Windows Script Host Version 5.8
                            Copyright (C) Microsoft Corporation. All rights reserved.

                            Please enter file name: yr2010.doc
                            Please enter character string: 22
                            Occurences of: 22 = 12

                            Have a Nice Day

                            vishuvishal



                              Beginner
                            • Thanked: 3
                              Re: how to get the Count of string in file
                              « Reply #14 on: August 08, 2010, 04:17:14 PM »
                              Victoria, I really understand wht these commands will do.

                              Seems like not a proper bat file