LoginSignup
0
0

More than 1 year has passed since last update.

【Linux】特定の行の前後に文字列を挿入する方法

Posted at

文字列を挿入

特定の文字列を含む行の上に挿入

特定の文字列を含む行の上に指定した文字列を挿入するには以下のように実行します。

$ sed '/line3/i INSERT' sample.txt
line1
line2
INSERT
line3
line4
line5

特定の文字列を含む行の下に挿入

特定の文字列を含む行の下に指定した文字列を挿入するには以下のように実行します。

$ sed '/line3/a INSERT' sample.txt
line1
line2
line3
INSERT
line4
line5

ファイルの内容を挿入

挿入する内容を記述したinsert.txtを以下のように作成します。

insert.txt
==========
  INSERT
==========

特定の文字列を含む行の上に挿入

特定の文字列を含む行の上に指定したファイルの内容を挿入するには以下のように実行します。

$ sed '/line3/e cat insert.txt' sample.txt
line1
line2
==========
  INSERT
==========
line3
line4
line5

特定の文字列を含む行の下に挿入

特定の文字列を含む行の下に指定したファイルの内容を挿入するには以下のように実行します。

$ sed '/line3/r insert.txt' sample.txt
line1
line2
line3
==========
  INSERT
==========
line4
line5

0
0
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
0
0