Page 1 of 1

Counting occurrences

Posted: Sat Jan 06, 2007 12:21 am
by gcotterl
How can I get TextPad to count the number of times "68" occurs in a file?

(Note: "68" may occur multiple times in a row).

Posted: Sat Jan 06, 2007 10:07 am
by ben_josephs
Find what: 68
Replace with: 68

Replace All
Look at the left-hand end of the status line. Undo the action to restore the previous state.

Posted: Sat Jan 12, 2008 6:29 pm
by Bob Hansen
I need a TextPad macro to insert the statistics including a count of string occurrences.

How to capture that count from the lower left corner and include it in the document with other specs inserted with Edit/Insert/Statistics?

I cannot find any command that returns a string count except in the status line.

Posted: Sat Jan 12, 2008 8:03 pm
by ben_josephs
There's no way to do this within TextPad that I'm aware of.

It's another job for a one-line script. E.g.

Code: Select all

perl -ne "$n ++ while /text_to_search_for/g ; END { print $n }" filename

Posted: Sat Jan 12, 2008 9:11 pm
by Bob Hansen
Thanks ben_josephs for confirming my fear.

Guess I could run the perl script as a tool, direct the result to a file and do a copy/paste all within the same macro. Will have to pull out my old perl stuff that I have not looked at in years.

Thanks for the suggestion.

Posted: Sat Jan 12, 2008 11:58 pm
by ben_josephs
Unfortunately, TextPad won't let you run a tool in a macro. Strange and sad, but true.

If you can get the tool to send its output to the clipboard you can do what you want in two steps: run the script and paste its result into your document.

You can get the script's output onto the clipboard either by redirecting it there (e.g., by calling Perl from 4NT (perl whatever.pl > clip:)) or by getting the script itself to send its output there (e.g. by using the Perl module Win32::Clipboard).

Alternatively, you can use a keyboard stuffer such as AutoIt or MacroScheduler to send key strokes to TextPad that run the tool and paste the output. Or you can do it from within the script itself, using the Perl module Win32::GuiTest.

Posted: Sun Jan 13, 2008 6:32 pm
by Bob Hansen
Thanks again for the options.

I already made a Macro Scheduler macro to read the file in TextPad, and to do the counting and pasting for me.