2
2

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

PHP 名前空間を変更した時に一括置換するtips(ripgrep, sd)

Last updated at Posted at 2020-06-18

ディレクトリ配下のファイル内の特定の文字列を再起的に置換したいことがよくあります。
grepsed を使っても良いですが今回は ripgrep と sd コマンドを組み合わせて一括置換します。

ripgrep

sd

置換する

Apple => Google に書き換えたい場合

> rg 'Apple' -l | xargs sd 'Apple' 'Google'

短く書けるので覚えられる!

置換する(grep, sed)

同じことをgrep, sedを使ってやるとこんな感じになります。

> grep -l 'Apple' ./* | xargs sed -i -e 's/Apple/Google/g'

すぐ忘れるので毎回置換方法をググってしまう...。

本題

名前空間を変更した時に一括置換

App\UseCase\HogeHoge から App\UseCase\Foo\Bar
移動した場合

> SEARCH='use App\\UseCase\\HogeHoge'
> REPLACE='use App\\UseCase\\Foo\\Bar'
> rg $SEARCH -l | xargs sd $SEARCH $REPLACE

名前空間は長くなりがちなので、変数作っていれておくといいかも。
\ は特殊文字なのでエスケープする。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?