LoginSignup
43
29

More than 5 years have passed since last update.

複数ファイルの文字列を一括置換

Last updated at Posted at 2015-03-18

LinuxやMacのターミナル上で、簡単に複数ファイルの文字列を一括置換出来る方法です。いつも失念してしまうので、備忘録として残します。

方法

具体的な方法としては下記のような処理を行います。

  • Grepで置換したい文字列を含むファイル一覧を取得
  • Perlを用いて文字列を実際に置換
grep -rl "beforeString" ./ | xargs perl -i -pe "s/beforeString/afterString/g"

補足

perlのオプションは下記の通りです。

オプション 機能
-e 文字列をスクリプトとして実行
-p 繰り返し処理と標準出力
-i ファイルを編集して、バックアップを作成
43
29
2

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
43
29