Mailinglist Archive: opensuse (3637 mails)

< Previous Next >
Re: [SLE] [OT DEVEL] How to switch spaces (or anything) for a newline with sed?
  • From: Alexandr Malusek <Alexandr.Malusek@xxxxxxxxxx>
  • Date: 30 May 2001 22:59:56 +0200
  • Message-id: <86lmnetuwj.fsf@xxxxxxxxxxxxxxxxxxxxx>
dieter <dieter@xxxxxxxxxxxxxxxx> writes:

> You can use the following to accomplish this:
> dieter:~$ cat <file> |sed 's/\ /\
> > /g'

It's OK, but the "cat" only increases system load. A better way is:

$ sed 's/\ /\
> /g' < file

> >From Jonathan Wilson to suse-linux-e@xxxxxxxxxxxxxx about [SLE] [OT DEVEL]...:
> > I've got a file that I need to convert all spaces to newlines.
> > According to the O'Rililey Regular Expressions book, it looks like \n
> > is the correct charachter

In 's/regexp/replacement/', \n is only valid in the "regexp", not in
the "replacement", see sed(1).

> > Obviously I don't know what I'm doing. What's the proper way to put in
> > things like \n, \r, and \t with sed?

Just put the corresponding character (not the backslash sequence) into
the "replacement". In bash, use <Ctrl-V> to avoid interpretation of
the character:

Instead of 's/ /\t/g' type 's/ /<Ctrl-v><TAB>/g'.
Instead of 's/ /\r/g' type 's/ /<Ctrl-v><Ctrl-m>/g'.

Perl's "replacement" syntax is better; backward compatibility is
preferred in sed.


< Previous Next >