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 5 years have passed since last update.

gnu-sedで特定文字列行の上にテキストを挿入する

Last updated at Posted at 2018-09-15

動機

設定ファイルを自動スクリプトで弄りたい時に困ったのでメモ
sedのeコマンドの情報ってほとんど見つからなかったし、意外と便利そうなので
eコマンドがgnu拡張らしいので、Macだと普通は動かない?かも

やり方

置換対象のファイル

$ cat << EOF > hoge.txt
1
2
specific text
3
4
EOF

挿入したいテキスト

$ cat << EOF > add.txt
text1
text2
EOF

sedでspecific textの上にadd.txtを挿入する。
ここでeコマンドを使うのがミソ

$ sed -i '/specific text/e cat add.txt' hoge.txt

specific textの上にadd.txtの内容が入る

$ cat hoge.txt
1
2
text1
text2
specific text
3
4

その他

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?