1.

Solve : Count how many times a number is seen in a text file.?

Answer»

Hi All,

Thanks for all your help in the past... Question: I want to count how many times a number is in a TEXT file then have it piped into another file.

Example: If it sees "016501" total the number of times and pipe it to another file.

Thanks again
Kenwhat exactly does the text file look like?

FBI have a batch file that looks at bigfile.txt that contains info like the following.

08-00417783,TX,75211,,,,,51B-X-Mapsco Page-Zone 016501-
08-00417783,TX,75211,,,,,51B-X-Mapsco Page-Zone 013512-
08-00417783,TX,75211,,,,,51B-X-Mapsco Page-Zone 016501-

Lines I have in my batch file
TYPE "bigfile.txt" | find "016501" >> "Master.txt"
type "bigfile.txt" | find "013512" >> "Master.txt"

I am trying to count how many times 016501 is in the file then pipe it into the bottom of the page of Master.txt. I would USE the same code for other numbers like 013512 etc... Thanks for the replyYou can use the FIND command to count. Something like:

Code: [Select]find /C "016501" "bigfile.txt" >> "Master.txt"
Or depending on the size and format of the files, you might be able make the count faster by just counting Master.txt instead of Bigfile.txt.
08-00417783,TX,75211,,,,,51B-X-Mapsco Page-Zone 016501-
08-00417783,TX,75211,,,,,51B-X-Mapsco Page-Zone 013512-
08-00417783,TX,75211,,,,,51B-X-Mapsco Page-Zone 016501-

So are you saying that, when it appears in a line, the 6 characters 016501 will only appear after Page-Zone, never at the beginning as part of the section that starts 08- ?

Thank you all for your help... This is the right direction I need.

This is the result I am getting using the line of code:
find /C "016501" "bigfile.txt" >> "count.txt"
find /C "013512" "bigfile.txt" >> "count.txt"

---------- BIGFILE.TXT: 21

---------- BIGFILE.TXT: 45

Is there a way to rename the following lines "---------- BIGFILE.TXT: 21" etc.. To "Number found for 016501 is: 21" ?.

Thanks for your help
KenQuote from: ktrueb on September 06, 2008, 11:57:55 AM

Is there a way to rename the following lines "---------- BIGFILE.TXT: 21" etc.. To "Number found for 016501 is: 21" ?.
Yes. You could do something like:
Code: [Select]for /f "tokens=2 delims=:" %%a in ('find.exe /C "016501" "bigfile.txt"') do (
echo Number found for 016501 is %%a >> "count.txt"
)Or to make it even more efficient, you could run all your numbers in another FOR loop like:
Code: [Select]for %%n in (016501 013512) do (
for /f "tokens=2 delims=:" %%a in ('find.exe /C "%%n" "bigfile.txt"') do (
type "bigfile.txt" | find.exe "%%n" >> "Master.txt"
echo Number found for %%n is %%a >> "count.txt"
)
)Sweet...!

That worked like a charm... Although I don't fully understand how that line works but I am GAINING knowledge everyday from this website and your help...

for /f "tokens=2 delims=:" %%a in ('find.exe /C "016501" "bigfile.txt"') do (
echo Number found for 016501 is %%a >> "count.txt"
)

Thank you again
Ken
Countme.bat

Code: [Select]@echo off
setlocal enabledelayedexpansion
set string=%1
set /a count=0
for /f "delims==" %%A in (bigfile.txt) do (
echo %%A | find "%string%">nul && set /a count=!count!+1
)
echo Found %string% !count! times
usage

Code: [Select]
S:\Test>if exist count.txt del count.txt

S:\Test>Countme 016501 >> count.txt
S:\Test>Countme 013512 >> count.txt
S:\Test\Batch\Countline>type count.txt
Found 016501 15 times
Found 013512 14 times

S:\Test>

Quote from: ktrueb on September 06, 2008, 12:22:05 PM
That worked like a charm... Although I don't fully understand how that line works but I am gaining knowledge everyday from this website and your help...

for /f "tokens=2 delims=:" %%a in ('find.exe /C "016501" "bigfile.txt"') do (
echo Number found for 016501 is %%a >> "count.txt"
)

  • FOR /f means "do a loop for each line of output in the command enclosed in ('')"
  • The "tokens=2 delims=:" means use the colon ":" character as the delimiter to break up a delimited line, and tokens=2 part means use the 2nd part of the delimited line. So in your case the output "---------- BIGFILE.TXT: 45" would be broken up by the delimiter of the colon so you would end up with 2 sections. Section 1 would be "---------- BIGFILE.TXT" and section 2 would be " 45" (with the colon in the middle).
  • The %%a is the variable where the FOR loop will store the output
  • The ('find.exe /C "016501" "bigfile.txt"') is the command the FOR loop will run
  • Then then last line just outputs the information we have broken up with the tokens and delimiters

I hope that helps you understand it better. You can also look at http://technet.microsoft.com/en-us/library/bb490909.aspx or try Code: [Select]FOR /? for more information.Thank you sir

Ken


Discussion

No Comment Found