views:

109

answers:

4

I would like to convert a text file containing words, space separate to a file where each word appears on a separate line

 sdf sdfsd= sdfsdf 
sdfsdf

will become

  sdf
  sdfsd= 
  sdfsdf 
  sdfsdf

thanks

+1  A: 

enter the following command:

:%s/ /\r/g

or whatever the carriage return is for your environment. \r for *nix, \n for windows.

coffeepac
'\r' is the right answer: when matching, end of line is '\n', when substituting end of line is '\r'. Regardless of the platform. VIM would handle the rest automatically. Only enhancement I would suggest is adding `\+` after space: `:%s/ \+/\r/g`
Dummy00001
@Dummy : good call about `+` (gonna add that to my answer).
Stephen
@Dummy rad, didn't know that \r and \n were platform independent. Thanks!
coffeepac
I often use `<C-V><C-M>` to type a newline, which then appears as `^%`.
Drasill
+3  A: 

Try this:

:%s/\s\+/\r/g

Explained:

:%     // whole file
 s     // substitute
 \s\+  // some number of whitespace characters
 \r    // for carriage return

Thanks to @Dummy00001 for the + idea.

Stephen
A: 

This is also easy to write as a shell script, you don't need sed or awk:

bash$ for word in $(cat input.txt); do echo "$word" >> output.txt; done
too much php
+1  A: 
$ tr " " "\n"<file|sed -n '/^$/!p'
sdf
sdfsd=
sdfsdf
sdfsdf
ghostdog74