• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

vi replace " " with return

I need to remove every space " " in a text file with a carrage return so that every word appears as a different line on the text.
0
Stoke
Asked:
Stoke
1 Solution
 
vinayacaCommented:
try this command in VI editor (Command mode)

:s/" "/^M/g

0
 
marecsCommented:
:%s/ /\n/g
0
 
StokeAuthor Commented:
Neither option works
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
marecsCommented:
What OS?

By not working do you mean nothing happened or the wrong thing happened?
0
 
StokeAuthor Commented:
Red hat 7.1
:%s/ /\n/g returns ^@ instead of the spaces, so something happens.
0
 
marecsCommented:
This is cheating a bit, but it works. I think the problem is that vi would lose its sence of line numbers if it allowed something to be replaced by a newline. It worked on vim under Windows though!

:%!tr " " "\n"

It actually makes use of the external program tr
0
 
StokeAuthor Commented:
Brilliant. That's exactly what I wanted.
0
 
tomasinoCommented:
it sounds like you were running gvim for windows if it displays ^@.  if you don't have the external program tr, you can still use the internal sed commands in vi on a windows machine by doing this:

:s/\ /\r/g

the \r is the newline character for PC.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now