Only some modern variants of sed support \n and other useful features. A traditional sed(1) (as still preserved on OpenBSD) does not support -i “in-place editing” nor even \n. These versions require a literal newline be inserted into the command line or script:
$ echo aaa | sed 's/a/\n/g'
nnn
$ echo aaa | sed 's/a/\\n/g'
\n\n\n
$ echo aaa | sed 's/a/\
/g' | od -bc
0000000 012 012 012 012
\n \n \n \n
0000004
Tim Maher in Minimal Perl discusses the evolution of sed and awk, including command line features stolen from Perl, such as \n support.
Technorati Tags: characters, Perl, Unix
For reference, the Mac OS X sed uses FreeBSD extensions:
STANDARDS
The sed utility is expected to be a superset
of the IEEE Std 1003.2 (``POSIX.2'')
specification.
The -E, -a and -i options are non-standard
FreeBSD extensions and may not be available
on other operating systems.
But does not support \n except as a literal newline:
$ echo aaa | sed 's/a/\n/g'
nnn
$ echo aaa | sed 's/a/\
/g' | od -bc
0000000 012 012 012 012
\n \n \n \n
0000004
For fun, here’s a perl snippet that converts \n or other escapes into literal characters:
$ echo -n '\n\n\n' \
| perl -pe 's/(\\.)/qq("$1")/eeg' \
| od -bc
0000000 012 012 012
\n \n \n
0000003
… and knowing is ⅛ the battle.