Community
    • Login

    Find and replace white space with tabs

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    10 Posts 5 Posters 3.0k 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.
    • Thomas AllisterT
      Thomas Allister
      last edited by

      Hi everybody I’m looking for help for this issue.

      I have a txt file with more than 7000 lines with this content:

      10 FIRST FOO
      1001 SECOND FOO
      100101 THIRD FOO
      10010101 FOURTH FOO
      10010102 EVEN MORE FOO
      10010103 STILL FOOING 
      11 BAR
      1102 SECOND BAR
      110201 THIRD BAR
      11020101 FOURTH BAR
      11020102 EVEN MORE FOURTH BAR
      

      and I need it to become like this:

      10		FIRST FOO
      1001		SECOND FOO
      100101		THIRD FOO
      10010101	FOURTH FOO
      10010102	EVEN MORE FOO
      10010103	STILL FOOING
      11		BAR
      1102		SECOND BAR
      110201		THIRD BAR
      11020101	FOURTH BAR
      11020102	EVEN MORE FOURTH BAR
      

      I thought that i can try to search for white space in third column 3, 5, 7 and 9 and replace it with three tabs , but when i run the search with regular expression like

      ^.{2}\s
      

      to find the blank in column 3 and then go to replace it, the editor replace all the characters from the start of the line, how can i replace only the character (or the whitespace) in column 3 (and 5, 7, 9)?

      Is there maybe another way to accomplish such result?

      TIA everybody for your attention.

      Alan KilbornA CoisesC 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Thomas Allister
        last edited by Alan Kilborn

        @Thomas-Allister

        Do:

        Find: ^\S+
        Replace: $0 and then add bunch of spaces after the 0
        Search mode: Regular expression

        You’ll obtain something like this:

        10                                                 FIRST FOO
        1001                                                 SECOND FOO
        100101                                                 THIRD FOO
        10010101                                                 FOURTH FOO
        10010102                                                 EVEN MORE FOO
        10010103                                                 STILL FOOING 
        11                                                 BAR
        1102                                                 SECOND BAR
        110201                                                 THIRD BAR
        11020101                                                 FOURTH BAR
        11020102                                                 EVEN MORE FOURTH BAR
        

        Then put your caret at some reasonable spot in the big bunch of spaces on line 1, and press Alt+Shift+b (requires N++ 8.5 or later). Note the column you are in from the N++ status bar.

        Move to the last line of the file, and get to the same column. Press Alt+Shift+b again.

        You’ll obtain:

        a74ff7e9-f0f5-4e5e-b874-4a9c6a4ef319-image.png

        Finally, press and hold Ctrl while you tap and release the Delete key, to get:

        4782673d-9c27-43b9-a797-51d6da43056d-image.png

        From there you can use the Backspace key or the space bar to reduce or increase the amount of space on all of the lines simultaneously.

        1 Reply Last reply Reply Quote 3
        • namx3249N
          namx3249
          last edited by

          Alan, sorry, but does not work for me …

          Clipboard01.png

          also Alt+Shift+b does not provide any events …
          i have latest 8.5.2 np++ version

          PeterJonesP Alan KilbornA 2 Replies Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @namx3249
            last edited by PeterJones

            @namx3249 ,

            sorry, but does not work for me

            My guess is that you didn’t notice the “then add a bunch of spaces after the 0”. Your replacement needs to be a long piece of text – the literal $0 then a bunch of spaces.

            Since spaces at the end of a regex replacement can be hard to see, I like using \x20 (which is regex speak for “ASCII HEX 20” which is the space character) when I post in the forum, like: $0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20

            also Alt+Shift+b does not provide any events …

            Yes it does. You just need to do two of them.

            012345
            67890A
            BCDEFG
            HIJKLM
            

            If you have your cursor between 0 and 1, then hit Alt+Shift+B, then move your cursor to between the L and the M and hit Alt+Shift+B, then you will have a rectangle selection from the 1 to the L.

            e86ac203-f5a4-4bdf-a5a3-84c0efe1bbd3-image.png

            After the first Alt+Shift+B, if you looked at the Edit menu, you would see a checkmark next to Begin/End Select in Column Mode, indicating that you’d done the begin but not yet done the end.
            a67811fb-29bf-4951-950e-453631b795cf-image.png

            1 Reply Last reply Reply Quote 1
            • Alan KilbornA
              Alan Kilborn @namx3249
              last edited by Alan Kilborn

              @namx3249 said in Find and replace white space with tabs:

              Alan, sorry, but does not work for me …

              Maybe there is some misinterpretation of what you actually have, and/or what you want.

              You said something about “three tabs” – do you really want to end up with tab characters in your file?

              1 Reply Last reply Reply Quote 0
              • CoisesC
                Coises @Thomas Allister
                last edited by Coises

                @Thomas-Allister said in Find and replace white space with tabs:

                I thought that i can try to search for white space in third column 3, 5, 7 and 9 and replace it with three tabs , but when i run the search with regular expression like

                ^.{2}\s
                

                to find the blank in column 3 and then go to replace it, the editor replace all the characters from the start of the line, how can i replace only the character (or the whitespace) in column 3 (and 5, 7, 9)?

                You want to use capture expressions, so that you include the original text in the replacement:

                Find: ^(.{2})\s
                Replace: \1\t

                The parentheses create a capture expression. They are numbered from 1 to 9 from the left (counting by where the opening parenthesis occurs, in case parentheses are nested). Capture expressions get more complicated, but that’s the simple version.

                It looks like you could do all your lines at once with:

                Find: ^(\d+)\s
                Replace: \1\t

                1 Reply Last reply Reply Quote 0
                • namx3249N
                  namx3249
                  last edited by namx3249

                  @PeterJones
                  thanks for you clarification. now i’ve understand. and all work fine for me too

                  @Alan-Kilborn

                  You said something about “three tabs” – do you really want to end up with tab characters in your file?

                  really i don’t understand what you’re referring to…

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

                    @namx3249 said in Find and replace white space with tabs:

                    really i don’t understand what you’re referring to…

                    Then it isn’t important, as long as everything is now working fine for you.

                    1 Reply Last reply Reply Quote 1
                    • namx3249N
                      namx3249
                      last edited by

                      so, the final solution is:
                      find: ^\S+
                      replace: $0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
                      put caret on blank space in a space between text in the first line, hold Alt+Shift+B (Edit menu - Begin/End Select in Column Mode), with arrow key put on the last line, hold again Alt+Shift+B
                      then with Ctrl + Del key all text on right side is aligned to the cursor. done

                      simply and interesting. thanks folks for this new trick

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

                        Just be careful not to do Ctrl+Delete in this situation:

                        df62cf80-cc96-4ac3-bcc4-9d29e53592a5-image.png

                        If you do, it will remove FIRST on line 1 and BAR on line 7.

                        Always have at least one space character on each line to the right of the “tall skinny” caret.

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors