Saved Bookmarks
| 1. |
Solve : find and findstr help? |
|
Answer» "find" does partly what I want, and "findstr" does partly what I want. But neither one separately provides fully what I want to accomplish. 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. 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. 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 FILES. If the two counts are the same, I am okay; if not, echo an error message. Is there a WAY to pipe find and findstr together in one statement? I can solve the problem with the following steps, but this seems like double work for each search: findstr/b "string1" output.txt > count1.txt findstr/b "string2" output.txt > count2.txt find/c "string1" count1.txt find/c "string2" count2.txt Thanks in advance. Code: [Select]echo off echo cat dog horse>output.txt echo cat dog horse>>output.txt echo cat dog horse>>output.txt echo cat dog horse>>output.txt echo man girl boy>>output.txt echo man girl boy>>output.txt echo man girl boy>>output.txt echo man girl boy>>output.txt echo dog man cat>>output.txt set string1=cat set string2=man setlocal enabledelayedexpansion set count1=0 set count2=0 for /f "delims=" %%L in (output.txt) do ( echo %%L | findstr /b "%string1%">nul && set /a count1+=1 echo %%L | findstr /b "%string2%">nul && set /a count2+=1 ) if %count1% equ %count2% ( echo Counts are equal ) else ( echo Counts are not equal ) echo Occurrences of %string1% at start of line = %count1% echo Occurrences of %string2% at start of line = %count2% Code: [Select]Counts are equal Occurrences of cat at start of line = 4 Occurrences of man at start of line = 4Thanks a lot.download grep for windows and then do this Code: [Select]C:\test>TYPE file cat dog horse cat dog horse cat dog horse cat dog horse man girl boy man girl boy man girl boy man girl boy dog man cat if you want to display strings "cat" or "man" (data stolen from ST), Code: [Select]C:\test>grep -E "^[[:SPACE:]]*(cat|man)" file cat dog horse cat dog horse cat dog horse cat dog horse man girl boy man girl boy man girl boy man girl boy to search "cat" or "man" at beginning and display count Code: [Select]C:\test>grep -Ec "^[[:space:]]*cat" file 4 C:\test>grep -Ec "^[[:space:]]*man" file 4 to get total count Code: [Select]C:\test>grep -Ec "^[[:space:]]*(cat|man)" file 8 Quote from: ghostdog74 on April 22, 2010, 05:34:24 PM download grep for windows and then do this . . . Or use wc -l. C:\>type output.txt cat dog horse cat dog horse cat dog horse cat dog horse man girl boy man girl boy man girl boy man girl boy dog man cat C:\>findstr "^cat" output.txt | E:\bin\wc -l 4 C:\>findstr "^man" output.txt | E:\bin\wc -l 4 C:\> |
|