Posix Sed で改行を出力する方法

Published: 2019/4/9


Posix Sed で改行を行う方法が、自明ではなかったのでまとめておく。

$ echo hogehoge | sed -e "$(printf 's/hogehoge/hoge\\\nhoge/g\n')"
hoge
hoge
$

この挙動は、 posix の sed command の説明ページに記載がある。

The application shall escape the <newline> in the replacement by preceding it by a <backslash>.

つまり、 replacement の newline は backslash でクォートするべき、とのこと。

参考: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html


Tags: posixsed

関連記事