To count a certain character in a long text file.

quapurna
2013-09-13
2013-09-17
  • quapurna
    quapurna
    2013-09-13

    Halo.
    I am a new user of Notepad++.

    Can I use notepad++ to count how many times the letter "a" appear in a long text file ?
    What feature should I use ?

    Thanks

     
  • Go to the Edit menu, press Find. Write "a" in the textbox. Press Count. The answer will appear in the statusbar of the find window.

     
  • quapurna
    quapurna
    2013-09-15

    Thank you, Andreas.
    I have found it.

    First of all, I am not a programmer.
    Can I make a macro (or something else) so that Notepad++ will make a new page (the search result) displaying how many times characters appear such as:

    ==========
    Search Result of (text file-name.txt)
    !=3
    @=1
    .....etc (symbol)
    1=5
    2=3
    .....etc (number)
    A=25
    a=300
    B=44
    b=75
    .....etc (letter)
    ==========

    Is there any macro-sample for the above purpose ?

    Thank you
    .

     
  • This is probably not possible to do using the macro system, but should be possible with a script (but that requires programming of course). There is a scripting subforum here where you could try to ask.

     
  • Loreia2
    Loreia2
    2013-09-16

    Hi,

    try to do it with a python script.
    Here is a small code demonstrator:
    http://www.sivachandran.in/2012/04/scripting-notepad-with-python.html

    crash course of counting in python:

    text = editor.getText()
    notepad.new()
    
    print "A=" + str(text.count("A"))
    

    Now, just do a loop over all characters you are interested in.

    for i in range(128):
        print chr(i) + =" + str(text.count(chr(i)))
    

    I can't test it right now, so feel free to report is something is broken. Also, feel free to report a full working code, it might help someone else.

    BR,
    Loreia

     
  • quapurna
    quapurna
    2013-09-17

    Thanks, Andreas and Loreia2.

    I read the following in the Scripting forum:
    "2. to provide support for scripting, using the 3 current scripting plugins - NppScripting (javascript), PHP Automation (PHP) and Python Script (Python)."

    I mentioned that I was not a programmer. I think I must make it clearer; I know nothing about programming. :(
    If I want to start learning one of those three scripts, which one should I learn and why ?
    On one computer, I use Windows7 and MS-Office 2010. On another one, I use Xubuntu 13.04 and LibreOffice4.1.1. With those 2 Operating systems I use, which one of the 3 scripts you suggest me to learn ?
    I am sorry if it is a stupid question.

    Thanks.

     
  • Loreia2
    Loreia2
    2013-09-17

    Hi,

    I mentioned that I was not a programmer. I think I must make it clearer; I know nothing about programming. :(

    No problem. This is just a simple script :-)
    Follow these steps:

    1. Plugins / Plugin manager / instal Python script
    2. After restart, go to Plugins / Python script / New Script
    3. Save script under some name, say counter.py
    4. copy /paste this text into it:

    .

    text = editor.getText()
    notepad.new()
    
    # change this constant to 'True' if you wish to include characters not found in the file
    INCLUDE_ZEROS = False
    
    for i in range(33,128):
        count = text.count(chr(i))
        if count > 0 or INCLUDE_ZEROS == True: 
            print chr(i) + "=" + str(count)
    
    text = ""
    

    .

    1. Plugins / Python script / configuration
    2. Select counter.py, click Add on Toolbar icons
    3. Click OK

    And that's it. You have a new button that automatically creates statistics for any active file.

    BR,
    Loreia

     
  • quapurna
    quapurna
    2013-09-17

    Thanks, Loreia2.

    Thank you for the script. It works.

    I changed the constant to 'True'. I also use ';' instead of '=' for the 'print' since I want to save the text in csv format to be opened and sorted with Excel.
    After the result is printed in Notepad++, I have to change 2 characters: " and ; to 'quote' and 'semicolon' since Excel reads them differently, but I can do that manually.

    Thanks.