

InterviewSolution
Saved Bookmarks
1. |
Solve : how to get the Count of string in file? |
Answer» <html><body><a href="https://interviewquestions.tuteehub.com/tag/hi-479908" style="font-weight:bold;" target="_blank" title="Click to know more about HI">HI</a>,<br/><br/>Am having a <a href="https://interviewquestions.tuteehub.com/tag/file-11330" style="font-weight:bold;" target="_blank" title="Click to know more about FILE">FILE</a> with 1 line having a file size of 35MB.<br/><br/><br/>Eg:- <br/>arun*America*MSC~INS*dfffs*Sdfsd*sdfsd~ssfsd*sdfsd~INS*dfffs*sdfsdf*sdfs~<br/><br/>I need to get a count of INS* in the above file. Am new to DOS Commands. <br/><br/>Please help me.<br/><br/>Thanks in Advance.<br/><br/>Regards,<br/>Arun S. Quote from: arunavlp on August 03, 2010, 04:43:17 AM<blockquote>hi,<br/><br/>Am having a file with 1 line having a file size of 35MB.<br/><br/><br/>Eg:- <br/>arun*America*MSC~INS*dfffs*Sdfsd*sdfsd~ssfsd*sdfsd~INS*dfffs*sdfsdf*sdfs~<br/><br/>I need to get a count of INS* in the above file. Am new to DOS Commands. <br/><br/>Please help me.<br/><br/>Thanks in Advance.<br/><br/>Regards,<br/>Arun S.<br/></blockquote> <br/>download <a href="http://gnuwin32.sourceforge.net/packages/gawk.htm">gawk f</a>or windows,<br/>then <br/> Code: <a>[Select]</a>c:\test> gawk "{m=gsub("INS",""); total+=m}END{print "total:" total}" file<br/>hi ,<br/><br/>Thanks for suggestion. but i got an error message like this <br/><br/>30.834<br/>gawk: {m=gsub(INS,");<br/>gawk: ^ unterminated string<br/><br/>i dont know wht this error means. Please help me on this.<br/><br/><br/>Regards,<br/>Arun S.Escape your double quotes<br/><br/> Code: <a>[Select]</a>c:\test> gawk "{m=gsub(\"INS\",\"\"); total+=m}END{print \"total:\" total}" file<br/>hi,<br/><br/>Thanks It works.. but please let me know if we can do it in Find Command.... <br/><br/>Regards,<br/>Arun S. Quote from: arunavlp on August 05, 2010, 12:34:57 AM<blockquote>hi,<br/><br/>Thanks It works.. but please let me know if we can do it in Find Command.... <br/><br/>Regards,<br/>Arun S.<br/></blockquote> 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 <a href="https://interviewquestions.tuteehub.com/tag/programming-1806" style="font-weight:bold;" target="_blank" title="Click to know more about PROGRAMMING">PROGRAMMING</a> is needed. ( that i will leave it someone else who has the expertise and time to show you, )<br/>When parsing files and doing string manipulation, use a good tool for the job.The <strong>find</strong> 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. <strong>Findstr</strong> does not do counting but allows for multiple search arguments and a limited form of regular expressions.<br/><br/>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). <br/><br/> Code: <a>[Select]</a>Const ForReading = 1<br/><br/>Set fso = CreateObject("Scripting.FileSystemObject")<br/><br/>Do<br/> WScript.StdOut.Write "Please enter file name: "<br/> strFile = WScript.StdIn.ReadLine<br/> If fso.FileExists(strFile) Then<br/> Set objFile = fso.OpenTextFile(strFile, ForReading)<br/> strCharacters = objFile.ReadAll<br/> Exit Do<br/> Else<br/> WScript.StdOut.Write "Invalid file name ... Try Again" & vbCrLf<br/> End If<br/>Loop<br/><br/>Do<br/> WScript.StdOut.Write "Please enter character string: "<br/> strToCount = WScript.StdIn.ReadLine<br/> If strToCount <> "" Then Exit Do <br/>Loop<br/><br/>strTemp = Replace(LCase(strCharacters), LCase(strToCount), "") <br/>WScript.Echo "Occurences of:", strToCount, "=", (Len(strCharacters) - Len(strTemp)) / Len(strToCount) <br/><br/>objFile.Close<br/><br/>Save the script with a <strong>vbs</strong> extension and run only from the command prompt as: <strong>cscript <em>scriptname.vbs</em></strong><br/><br/>Good luck. <br/>I can give you Idea what it should like to be:<br/><br/>set /p pass= echo %pass%<br/>call set new=%%pass:~%a%,1%%<br/>set /a a=%a% + 1<br/> set key=%key%%new%<br/>echo %new%<br/><br/>This new will give you the number of string.<br/>However, I am going will give you further details tommorrow<br/><br/>Thanks and regard <br/>vishu Code: <a>[Select]</a>set /p pass=<string.txt<br/>echo %pass%<br/>:st<br/>call set new=%%pass:~%a%,1%%<br/>echo a=%a% + 1<br/>echo %a%<br/>set key=%key%%new%<br/>echo %new%<br/>echo %key%<br/>pause<br/>::if %new% ==; goto :EOF<br/><br/>pause<br/>goto :st<br/><br/><br/>All we need to fix is loop.<br/>Change the string.txt to your file drive:path\file name<br/>Gave you a best option<br/><br/>echo off<br/><br/>sed s/the/the\\n/g yz.txt | egrep -c the<br/><br/><br/><br/>counthe.bat<br/>10<br/>type yz.txt<br/>the<br/>the<br/>the<br/>the<br/>the the the<br/>the the the<br/>Two \\ should be one<br/><br/>C:\\test>type cntstr.bat<br/><a href="https://interviewquestions.tuteehub.com/tag/rem-613824" style="font-weight:bold;" target="_blank" title="Click to know more about REM">REM</a> echo off<br/>sed s/%1/%1\\n/g %2 | egrep -c %1<br/><br/>C:\\test>cntstr.bat the yz.txt<br/><br/>C:\\test>rem echo off<br/><br/>C:\\test>sed s/the/the\\n/g yz.txt | egrep -c the<br/>10<br/><br/>C:\\test>type yz.txt<br/>the<br/>the<br/>the<br/>the<br/>the the the<br/>the the the<br/>Only one \\ backslash each time<br/><br/>type cntstr.bat<br/>rem echo off<br/>sed s/%1/%1\\n/g %2 | egrep -c %1<br/><br/>cntstr.bat 22 yr2010.doc<br/><br/>rem echo off<br/><br/>sed s/22/22\\n/g yr2010.doc | egrep -c 22<br/>12<br/><br/>Output for <a href="https://interviewquestions.tuteehub.com/tag/reply-1185278" style="font-weight:bold;" target="_blank" title="Click to know more about REPLY">REPLY</a> #6 by sidewinder<br/><br/><br/>cscript swcnt.vbs<br/>Microsoft (R) Windows Script Host Version 5.8<br/>Copyright (C) Microsoft Corporation. All rights reserved.<br/><br/>Please enter file name: yr2010.doc<br/>Please enter character string: 22<br/>Occurences of: 22 = 12<br/><br/>Victoria, I really understand wht these commands will do.<br/><br/>Seems like not a proper bat file</body></html> | |