Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to replace space with newline

5 views
Skip to first unread message

Robert G. Jacobs

unread,
Mar 31, 1998, 3:00:00 AM3/31/98
to

How can I use sed to replace all the spaces in a file with newlines? A
'\n' matches the newline in the original (to be replaced) text, but not
for the replacement text.

Rob


Eric Fulton

unread,
Mar 31, 1998, 3:00:00 AM3/31/98
to
try using [CTRL-V][CTRL-M]

Eric

John Andrea

unread,
Apr 2, 1998, 3:00:00 AM4/2/98
to

> How can I use sed to replace all the spaces in a file with
> newlines? A

>> Something like:
>> tr "[:space:]" "\n" < somefile.txt > somefile.out
>> should do the trick

tr often doesn't like special characters, instead use the octal value

tr ' ' '\012' <input >output

SteveLiu

unread,
Apr 4, 1998, 3:00:00 AM4/4/98
to Robert G. Jacobs

In editor like vi,
use :
:1,$s/ /^M/g

or with tr:

tr ' ' '\n' <yourfile >newfile

or with perl:
open(FILE, "yourfilename");
open(NFILE, "newfilename");
while (<FILE>){
$_ =~ s/\s+/\n/g;
print NFILE $_;
}
close(FILE);
close(NFILE);


Robert G. Jacobs wrote:

> How can I use sed to replace all the spaces in a file with newlines? A

SteveLiu

unread,
Apr 4, 1998, 3:00:00 AM4/4/98
to Robert G. Jacobs

In editor like vi,
use :
:1,$s/ /^M/g

or with tr:

tr ' ' '\n' <yourfile >newfile

or with perl:
open(FILE, "yourfilename");
open(NFILE, "newfilename");
while (<FILE>){
$_ =~ s/\s+/\n/g;
print NFILE $_;
}
close(FILE);
close(NFILE);

=================
Fly Like An Eagle
=================

Villy Kruse

unread,
Apr 8, 1998, 3:00:00 AM4/8/98
to

If you use classic vi (not vim) you might need to do it this way:


Q
s/ /\
/g

vi

The Q command puts you in ex mode; you cet a colon prompt. You can then
issue the s command replacing space with new line. Yhe new line must be
escaped with a backslash. Type vi to get back to vi mode.

Villy

Sajjad Lateef

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

In vi, I always escape the Control-M by typing in Control-V (NUL)
character beforehand. Else it does not work (at least for me). The other
solutions look allright.

Sajjad

SteveLiu (khon...@nease.net) wrote:
: In editor like vi,

Chris Mikkelson

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

saj...@uic.edu (Sajjad Lateef) writes:

> In vi, I always escape the Control-M by typing in Control-V (NUL)
> character beforehand. Else it does not work (at least for me). The other
> solutions look allright.
>
> Sajjad
>
> SteveLiu (khon...@nease.net) wrote:
> : In editor like vi,
> : use :
> : :1,$s/ /^M/g
>
> : or with tr:
>
> : tr ' ' '\n' <yourfile >newfile
>

Or in vi:

:%!tr ' ' '\n'

Sorry, just had to get that one in ;-)

0 new messages