2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Linux】sedコマンドで改行を置換する方法

Posted at

改行を置き換える方法

zオプションを使用することで改行を置き換えられます。

zオプションを使用しない場合には次のようになります。

$ cat sample.txt                            
sample
example
test

$ sed 's/sample//' sample.txt

example
test

sampleが削除されますが改行が残っているため空白行が出力されます。

zオプションを使用すると次のようになります。

$ cat sample.txt                            
sample
example
test

$ sed -z 's/sample\n//' sample.txt
example
test

改行も含めて置き換えされたため空白行が削除されています。

上書きする

上書きする場合にはiオプションを使用して次のように実行します。

sed -i -z 's/sample\n//' sample.txt
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?