Note: For brevity, the commands use here-strings (<<<
) and ANSI C-quoted strings ($'...'
). Both these shell features work in bash
, ksh
, and zsh
.
# GNU Sed
$ sed ':a;$!{N;ba}; s/\n/\t/g' <<<$'line_1\nline_2\nline_3'
line_1 line_2 line_3
# BSD Sed equivalent (multi-line form)
sed <<<$'line_1\nline_2\nline_3' '
:a
$!{N;ba
}; s/\n/'$'\t''/g'
# BSD Sed equivalent (single-line form, via separate -e options)
sed -e ':a' -e '$!{N;ba' -e '}; s/\n/'$'\t''/g' <<<$'line 1\nline 2\nline 3'
BSD Sed notes:
Note the need to terminate labels (:a
) and branching commands (ba
) either with actual newlines or with separate -e
options.
Since control-character escape sequences such as \t
aren't supported in the replacement string, an ANSI C-quoted tab literal is spliced into the replacement string.
(In the regex part, BSD Sed only recognizes \n
as an escape sequence).