0
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 のターミナル上からコマンドで"ファイルの最後に行を挿入"

Posted at

Linux のターミナル上からコマンドで"ファイルの最後に行を挿入"する方法

結論

sedのaコマンドを利用して行を挿入する

$ sed -e '$a 1行目の文字列\n2行目の文字列\n3行目の文字列' 入力ファイル

例えば

以下のfile.txtの最終行に「4.black」を入れたい場合 ↓

$ cat file.txt 

1.red
2.brue
3.pink

このコマンドを実行することで

$ sed -e '$a 4.black' file.txt 

このように最終行に挿入が可能となる。

$ cat file.txt 

1.red
2.brue
3.pink
4.black

複数行の場合

以下のfile.txtの最終行に「4.black」の次に「5.white」を入れたい場合 ↓

$ cat file.txt 

1.red
2.brue
3.pink

このコマンドを実行することで

$ sed -e '$a 4.black\n5.white' file.txt 

このように最終行に挿入が可能となる。

$ cat file.txt 

1.red
2.brue
3.pink
4.black
5.white
0
1
1

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