Find&Replace in one column

  • I have a large text file with lots of lines, each lines representing info on one thing, in specific order, in this format:

    Lets say, I want to replace all occurence of Steve with Stephan in the first column for whatever reason. First I make notepad sparate the file into actual colums using TextFX. Then I select the first column using Alt-Shift-leftclick or Begin/End Select. So I have the first column selected already. Then I go to Find&Replace and it has this small box called Replace only in selected text, which I need to check in for my plan to work, however it does not allow me to do so. What can I do?

    Sidenote: Don’t suggest any drastically different solution. Steve may appear anywhere in the text and if I do something just a little bit wrongly, it’d also get replaced and I wouldn’t even know about it, but I only want to replace it in that one column.

  • Hi @Gergely-Apró

    Yes, the Find&Replace feature doesn’t work on rectangular selections.

    Despite your sidenote, I will suggest you a regex approach in order to get the following: replace all the Steve appearances as long as they are placed between the start of the line (^) and the first pipe (|) :

    Search: (?-s)^(Steve)(?=\|)
    Replace: Stephan

    Put the caret at the very beginning of the document, select just the Regular Expressions mode and click on Replace All.

    Given the following data


    the outcome of the regex replacement is


    Please note that the Steve string at lines four and six wasn’t replaced.

    Have fun!

  • I’ll test that but I’d still prefer if I could make it work my way somehow

  • @Gergely-Apró said in Find&Replace in one column:

    Don’t suggest any drastically different solution.

    I’d still prefer if I could make it work my way somehow

    Hmmm, I think it is the difference between “not being able to do something at all” and perhaps “learning a bit of something about how you can easily do it”. Are you paralyzed with fear about that second choice?

    I’d point out that the “easy” way doesn’t require bastardizing your data with an obsolete plugin (TextFX) first (which gets you nowhere anyway).

  • @Alan-Kilborn I understand, I’m usually not this much against new stuff, but I’ve been working on something for 9 months now, little by little, and when I started I had zero knowledge about what I’m doing. I actually still don’t really know. So every minor thing that didn’t work, no matter how trivial it was to solve it, took me a week to figure out, and brought me closer to this attitude.

    I’ve also been told a few times that the way I am doing it is inefficient, but at least it works. Why I use textFX? To visible divide my text into columns. Which allows me to select a column. After which I can move the column elsewhere or try to do Find&replace in it (didn’t know it won’t work). Is there a better/easier/faster way to do it? Maybe, but after such a long time, I got tired of looking for the best alternative for everything.

  • @Gergely-Apró

    Well, I certainly don’t advocate agonizing over what’s “best”.
    I suppose with experience (even a little), things either start to feel “right” or maybe “heavy”.
    Thus, you keep the “right” ones and discard the “heavy”.

    I am certainly sorry that Notepad++ can’t do a “column” find/replace.
    It is only very recently that it got the ability to find text in a stream (non-column) selection!

    I’m hesitant to bring this up, but for myself, I’ve been doing column-block replacements using Notepad++ for a long time, maybe years. How do I do this? Well, I have a scripting plugin, and I have written a script to do it. Yes, yes, I know, that overwhelms you, but I’ll show the technique and maybe it benefits someone else.

    First, we use the Pythonscript plugin, and write some Python code to create a function that, when called, can replace text in a column block (or a regular block, or several regular blocks (called multi-selection):

    # -*- coding: utf-8 -*-
    from Npp import notepad, editor
    def editor_rereplace(editor_x, find_what, replace_with, ignore_case_flag=0, max_count=0, start_end_pos_tup=None):
        # if start_end_pos_tup is None, replace will be done in all active selections
        leave_caret_pos = editor_x.getAnchor()
        if editor_x.getCurrentPos() < leave_caret_pos: leave_caret_pos = editor_x.getCurrentPos()
        start_end_pos_tup_list = []
        if start_end_pos_tup == None:
            if editor_x.getSelectionEmpty():
                start_end_pos_tup_list.append((0, editor_x.getTextLength()))
                for n in range(editor_x.getSelections()):
                    (s, e) = (editor_x.getSelectionNStart(n), editor_x.getSelectionNEnd(n))
                    if s != e:
                        if s < leave_caret_pos: leave_caret_pos = s
                        start_end_pos_tup_list.append((s, e))
        start_end_pos_tup_list.reverse()  # process data closest to bottom first to avoid messing with earlier match positions
        for (s, e) in start_end_pos_tup_list: editor_x.rereplace(find_what, replace_with, ignore_case_flag, s, e, max_count)
        editor_x.setEmptySelection(leave_caret_pos)  # active selection(s) after replace can be wacky, so..

    Next, we create some code that calls this function, which in this case is your typical “what do you want to find, what do you want to replace it with” type stuff:

    findwhat = notepad.prompt('Enter find string:', '', '')
    if findwhat != None and len(findwhat) > 0:
        replacewith = notepad.prompt('Enter replace string:', '', '')
        if replacewith != None and len(replacewith) > 0:
            editor_rereplace(editor, findwhat, replacewith)

    This script, when run, prompts thusly:


    After typing some text to search for and clicking OK, it prompts this way:


    After some text is entered and OK is pressed, the script goes on to replace text, in the selected area, be it any type of selection (or the whole document if no selection was active when run).

Log in to reply