Community
    • Login

    How to find/replace a character in every specific line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 4.6k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • johnny27J Offline
      johnny27
      last edited by

      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

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA Offline
        Alan Kilborn @johnny27
        last edited by

        @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.

        1 Reply Last reply Reply Quote 1
        • guy038G Offline
          guy038
          last edited by guy038

          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

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors