Search code examples
pythonregexnotepad++

Add a value and sum it up using Notepad++


I am having a file wherein the data is similar as given below:

abc - $0.05
xyz - $0.01
rst - $0.09
etc - $0.4

What I want to do is, increment all the values by $0.02 so that the final sum will look like below:

abc - $0.07
xyz - $0.12
rst - $0.11
etc - $0.42

How can I achieve this using Notepad++

Thank you.


Solution

  • You can run a python script within the PythonScript plugin.

    If it is not yet installed, follow this guide

    Create a script (Plugins >> PythonScript >> New Script)

    Copy this code and save the file (for example calculate.py):

    import re
    def calculate(match):
        return '%s' % (str(float(match.group(1)) + 0.02))
    
    editor.rereplace('(\d+\.\d+)', calculate)
    
    • Open the file you want to change
    • Run the script (Plugins >> PythonScript >> Scripts >> calculate)
    • Done

    Result for given example:

    abc - $0.07
    xyz - $0.03
    rst - $0.11
    etc - $0.42
    

    Note the difference from your expected second line (xyz - $0.01 becomes xyz - $0.03 and not xyz - $0.12)