Split line in text file



  • 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



  • 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”



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



  • 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



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


Log in to reply