How to find/replace a character in every specific line



  • Hi,
    assume i have 500,000 line ends with comma (,) like this:

    test,'number',null,
    test,'number',null,
    test,'number',null,
    

    i want to find/replace comma (,) with (;) in the end of for example every 50,000 line:

    line 1: test,'number',null,
    line 50: test,'number',null,
    line 50,000: test,'number',null;
    

    is that possible to do with nodepad++ ?

    thanks in advance



  • @johnny27

    Here’s an example that will do it for every fifth line:

    Open the Replace dialog by pressing Ctrl+h and then set up the following search parameters:

    Find what box: (?-s)((?:^.+\R){4}^.+),
    Replace with box: \1;
    Search mode radiobutton: Regular expression
    Wrap around checkbox: ticked

    Then press the Replace All button.

    The 4 in the search expression is key to doing every 5th line. Adjust for your situation.



  • Hello, @johnny27 and All,

    Interesting problem ! And easy to solve with, both, regular expressions and the Column Editor ;-))

    Here is the road map :

    • Open your file in Notepad++

    • Place th caret at the very beginning of the first line

    • Open the Column Editor ( Alt + C )

    • Select Number to Insert

    • Type in 1 in all zones

    • Tick the Leading zeros option ( IMPORTANT )

    • Select the Dec format, if necessary

    • Click on the OK button

    => Each line should be preceded with a 6 digits number !


    • Now, open the Replace dialog ( Ctrl + H )

    • SEARCH (?-s)^.[50]0000(.+),|^\d{6}

    • REPLACE ?1\1;

    • Tick the Wrap around option

    • Select the Regular expression search mode

    • Click on the Replace All button

    Voila ! Nice isn’t it ?


    Notes :

    • The search regex contains two alternatives :

      • First, the (?-s) in-line modifier ensures that any . regex symbol corresponds to a single standard character, only and not to a line-break char !

      • Then the part ^.[50]0000 searches for any number of six digits, beginning current line and containing a 0 or a 5 at second position, followed with four 0 digits

      • And the part (.+), looks for theremainder of the lines, minus the , character, which is stored as group 1, due to the parentheses

    • If current line number is not of the form ^.[50]0000, then it, necessarily, matches the second alternative :

      • The part ^\d{6} matches the 6 digits number, generated by the Column Editor, which begins any line

    • The replacement regex contains a conditional replacement (?#....:....) :

      • If group 1 exists ( every 50,000 lines ), we rewrites the group 1, followed with a semi-colon

      • If group 1 is absent, as the negative part, after a : does not exist, the first 6 digits number of any line are simply deleted

    Best Regards,

    guy038


Log in to reply