

InterviewSolution
Saved Bookmarks
1. |
Solve : find and findstr help? |
Answer» <html><body><br/>"find" does partly what I want, and "findstr" does partly what I want. But neither one separately provides fully what I want to accomplish.<br/><br/>I want to count the occurrences of string1 in a text file output.txt, only if the string is at the beginning of the line. I do not want to see the lines scroll on the screen, only the number of occurrences.<br/><br/>Then I want to count the occurrences of string2 in the same file output.txt, again only at the beginning of the line and displaying only the count.<br/><br/>Then I want to compare the two counts, either with a visual check on the screen or with the comp command if I chose to send the results to two text <a href="https://interviewquestions.tuteehub.com/tag/files-20889" style="font-weight:bold;" target="_blank" title="Click to know more about FILES">FILES</a>. If the two counts are the same, I am okay; if not, echo an error message.<br/><br/>Is there a <a href="https://interviewquestions.tuteehub.com/tag/way-246442" style="font-weight:bold;" target="_blank" title="Click to know more about WAY">WAY</a> to pipe find and findstr together in one statement?<br/><br/>I can solve the problem with the following steps, but this seems like double work for each search:<br/><br/><br/>findstr/b "string1" output.txt > count1.txt<br/>findstr/b "string2" output.txt > count2.txt<br/>find/c "string1" count1.txt<br/>find/c "string2" count2.txt<br/><br/><br/>Thanks in advance. Code: <a>[Select]</a>echo off<br/><br/>echo cat dog horse>output.txt<br/>echo cat dog horse>>output.txt<br/>echo cat dog horse>>output.txt<br/>echo cat dog horse>>output.txt<br/>echo man girl boy>>output.txt<br/>echo man girl boy>>output.txt<br/>echo man girl boy>>output.txt<br/>echo man girl boy>>output.txt<br/>echo dog man cat>>output.txt<br/><br/>set string1=cat<br/>set string2=man<br/><br/>setlocal enabledelayedexpansion<br/>set count1=0<br/>set count2=0<br/>for /f "delims=" %%<a href="https://interviewquestions.tuteehub.com/tag/l-236827" style="font-weight:bold;" target="_blank" title="Click to know more about L">L</a> in (output.txt) do (<br/> echo %%L | findstr /b "%string1%">nul && set /a count1+=1<br/> echo %%L | findstr /b "%string2%">nul && set /a count2+=1<br/> )<br/><br/>if %count1% equ %count2% (<br/> echo Counts are equal<br/>) else (<br/> echo Counts are not equal<br/>)<br/><br/>echo Occurrences of %string1% at start of line = %count1%<br/>echo Occurrences of %string2% at start of line = %count2% <br/> Code: <a>[Select]</a>Counts are equal<br/>Occurrences of cat at start of line = 4<br/>Occurrences of man at start of line = 4Thanks a lot.download <a href="http://gnuwin32.sourceforge.net/packages/grep.htm">grep for windows</a> and then do this<br/><br/> Code: <a>[Select]</a>C:\test><a href="https://interviewquestions.tuteehub.com/tag/type-238192" style="font-weight:bold;" target="_blank" title="Click to know more about TYPE">TYPE</a> file<br/>cat dog horse<br/> cat dog horse<br/>cat dog horse<br/> cat dog horse<br/> man girl boy<br/> man girl boy<br/>man girl boy<br/>man girl boy<br/>dog man cat<br/><br/>if you want to display strings "cat" or "man" (data stolen from ST), <br/> Code: <a>[Select]</a>C:\test>grep -E "^[[:<a href="https://interviewquestions.tuteehub.com/tag/space-239477" style="font-weight:bold;" target="_blank" title="Click to know more about SPACE">SPACE</a>:]]*(cat|man)" file<br/>cat dog horse<br/> cat dog horse<br/>cat dog horse<br/> cat dog horse<br/> man girl boy<br/> man girl boy<br/>man girl boy<br/>man girl boy<br/><br/>to search "cat" or "man" at beginning and display count<br/> Code: <a>[Select]</a>C:\test>grep -Ec "^[[:space:]]*cat" file<br/>4<br/><br/>C:\test>grep -Ec "^[[:space:]]*man" file<br/>4<br/><br/>to get total count<br/> Code: <a>[Select]</a>C:\test>grep -Ec "^[[:space:]]*(cat|man)" file<br/>8<br/> Quote from: ghostdog74 on April 22, 2010, 05:34:24 PM<blockquote>download <a href="http://gnuwin32.sourceforge.net/packages/grep.htm">grep for windows</a> and then do this . . .<br/><br/> </blockquote> <br/>Or use wc -l.<br/><br/>C:\>type output.txt<br/>cat dog horse<br/>cat dog horse<br/>cat dog horse<br/>cat dog horse<br/>man girl boy<br/>man girl boy<br/>man girl boy<br/>man girl boy<br/>dog man cat<br/><br/>C:\>findstr "^cat" output.txt | E:\bin\wc -l<br/> 4<br/><br/>C:\>findstr "^man" output.txt | E:\bin\wc -l<br/> 4<br/><br/>C:\><br/></body></html> | |