Home / Microsoft / Microsoft DOS / how to get the Count of string in file
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 3 ... 8 - (Bottom) Print
Author Topic: how to get the Count of string in file  (Read 7671 times)
arunavlp
Topic Starter
Greenhorn



Posts: 5


« 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.
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« 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
IP logged

arunavlp
Topic Starter
Greenhorn



Posts: 5


« 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.
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« 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
IP logged

arunavlp
Topic Starter
Greenhorn



Posts: 5


« 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.
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« 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.
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,341

Experience: Familiar
OS: Windows 7

« 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
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
vishuvishal
Beginner



Thanked: 3
Posts: 77




« 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
IP logged
vishuvishal
Beginner



Thanked: 3
Posts: 77




« 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

IP logged
victoria
Beginner



Thanked: 1
Posts: 69


« 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 » IP logged

Have a Nice Day
victoria
Beginner



Thanked: 1
Posts: 69


« 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
IP logged

Have a Nice Day
victoria
Beginner



Thanked: 1
Posts: 69


« 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 » IP logged

Have a Nice Day
victoria
Beginner



Thanked: 1
Posts: 69


« Reply #12 on: August 08, 2010, 03:47:04 PM »

IP logged

Have a Nice Day
victoria
Beginner



Thanked: 1
Posts: 69


« 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

IP logged

Have a Nice Day
vishuvishal
Beginner



Thanked: 3
Posts: 77




« 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
IP logged
Pages: [1] 2 3 ... 8 - (Top) Print 
Home / Microsoft / Microsoft DOS / how to get the Count of string in file « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.108 seconds with 20 queries.