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.
    • Yasser KhalilY
      Yasser Khalil
      last edited by

      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
      • Yasser KhalilY
        Yasser Khalil
        last edited by

        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”

        Scott SumnerS 1 Reply Last reply Reply Quote 0
        • Scott SumnerS
          Scott Sumner @Yasser Khalil
          last edited by

          @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
          • guy038G
            guy038
            last edited by

            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 S RawatV
              V S Rawat
              last edited by

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

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