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

バックアップを取ってファイルを上書き(sedによる処理)

Last updated at Posted at 2019-10-29

ファイルにsedで処理を施して上書きしたい際に,同時にバックアップを取りたければ,

sed -i.bak -e {sedによる処理} {ファイル名}

とすれば良い.
また,ファイルは複数指定可能.

例 : カレントディレクトリ配下のtest1.txtとtest2.txtの2行目のアンダースコアを削除したい

実行前のディレクトリ構成
.
├─ test1.txt
└─ test2.txt

実行コード
sed -i.bak -e '2s/_//g' test1.txt test2.txt

実行結果
.
├── test1.txt
├── test1.txt.bak
├── test2.txt
└── test2.txt.bak

.bakファイルの中身は書き換える前のものが入っている.

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