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 exampleThis would be:
This is
just a
sample text
for exampleThanks 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” -
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 exampleor, even, in a classic displaying, but, let’s say, every three words per line, as below :
This is just a sample text for exampleIt does not act, as expected :-((
So, I propose this very similar S/R :
SEARCH
(\w+)\s+(\w+)\s*REPLACE
\1\x20\2\r\nWhich 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
\1and\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\nCheers,
guy038
-
try plugin TEXTFX, you will find many easy methods for such manipulations.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login