• Login
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.
  • T
    Thomas Allister
    last edited by Apr 21, 2023, 8:57 AM

    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.

    A C 2 Replies Last reply Apr 21, 2023, 11:28 AM Reply Quote 0
    • A
      Alan Kilborn @Thomas Allister
      last edited by Alan Kilborn Apr 21, 2023, 12:46 PM Apr 21, 2023, 11:28 AM

      @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
      • N
        namx3249
        last edited by Apr 21, 2023, 4:53 PM

        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

        P A 2 Replies Last reply Apr 21, 2023, 5:02 PM Reply Quote 0
        • P
          PeterJones @namx3249
          last edited by PeterJones Apr 22, 2023, 3:47 PM Apr 21, 2023, 5:02 PM

          @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
          • A
            Alan Kilborn @namx3249
            last edited by Alan Kilborn Apr 21, 2023, 5:15 PM Apr 21, 2023, 5:06 PM

            @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
            • C
              Coises @Thomas Allister
              last edited by Coises Apr 21, 2023, 5:11 PM Apr 21, 2023, 5:09 PM

              @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
              • N
                namx3249
                last edited by namx3249 Apr 22, 2023, 7:22 AM Apr 22, 2023, 7:21 AM

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

                A 1 Reply Last reply Apr 22, 2023, 10:21 AM Reply Quote 0
                • A
                  Alan Kilborn @namx3249
                  last edited by Apr 22, 2023, 10:21 AM

                  @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
                  • N
                    namx3249
                    last edited by Apr 22, 2023, 12:59 PM

                    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
                    • A
                      Alan Kilborn
                      last edited by Apr 22, 2023, 1:30 PM

                      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
                      5 out of 10
                      • First post
                        5/10
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors