Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

方法

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

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

補足

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

オプション 機能
-e 文字列をスクリプトとして実行
-p 繰り返し処理と標準出力
-i ファイルを編集して、バックアップを作成
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away