1.

Solve : how to get the Count of string in file?

Answer» 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. Quote from: arunavlp 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.

download  gawk for windows,
then
Code: [Select]c:\test> gawk "{m=gsub("INS",""); total+=m}END{print "total:" total}" file
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.Escape your double quotes

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

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

Regards,
Arun S. Quote from: arunavlp 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.
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.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).

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. 
I can give you Idea what it should like to be:

set /p pass= 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 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

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
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
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

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

Victoria, I really understand wht these commands will do.

Seems like not a proper bat file


Discussion

No Comment Found