• Login
Community
  • Login

Split line in text file

Scheduled Pinned Locked Moved General Discussion
5 Posts 4 Posters 6.3k 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.
  • Y
    Yasser Khalil
    last edited by Nov 19, 2017, 11:19 AM

    Hello everyone

    I have a text file with some lines and I need a way to split each line to two words
    Example:
    This is just a sample text for example

    This would be:
    This is
    just a
    sample text
    for example

    Thanks advanced for help

    1 Reply Last reply Reply Quote 0
    • Y
      Yasser Khalil
      last edited by Nov 19, 2017, 12:01 PM

      After some searches I can split each word in line in that way using
      Ctrl + H >> Checking “Regular expression” >> In find box I typed " \s*" and in replace box I typed " \n" then clicked “Replace All”

      S 1 Reply Last reply Nov 19, 2017, 1:53 PM Reply Quote 0
      • S
        Scott Sumner @Yasser Khalil
        last edited by Nov 19, 2017, 1:53 PM

        @Yasser-Khalil

        Here’s a simplistic approach that meets your original goal:

        Find-what zone: (\w+\s+\w+)\s+
        Replace-with zone: \1\r\n

        1 Reply Last reply Reply Quote 0
        • G
          guy038
          last edited by Nov 20, 2017, 7:06 PM

          Hi, @yasser-khalil, @Scott-sumner and All,

          Scott, your regex works fine, as long as the text is located on a single line. But if text is, for instance, multi-lines :

          This
           is 
              just
          a 
            sample
                    text 
          for
               example 
          

          or, even, in a classic displaying, but, let’s say, every three words per line, as below :

          This is just
          a sample text
          for example
          

          It does not act, as expected :-((

          So, I propose this very similar S/R :

          SEARCH (\w+)\s+(\w+)\s*

          REPLACE \1\x20\2\r\n

          Which should works, as long as the list of all the individual words wanted, is a unique block of text, only separated by horizontal / vertical space characters !

          A second advantage about setting each word of the couple, as groups \1 and \2, is that you may separate them by any character / string. For instance, the characters -, +, |…, the string <--->… :-)


          Important : Yasser, in your second post, you said :

          and in replace box I typed " \n"

          So I presume that you’re using Unix files. In that case, change the sequence \r\n, at the end of the replace regex, by the simple Line Feed character \n

          Cheers,

          guy038

          1 Reply Last reply Reply Quote 0
          • V
            V S Rawat
            last edited by Nov 24, 2017, 6:17 AM

            try plugin TEXTFX, you will find many easy methods for such manipulations.

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