0
0

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.

git grep の検索結果から #perl でファイルのテキストを #正規表現 + キャプチャ で一斉置換する ( #git ) ( sed は正規表現が貧弱だし )

0
Last updated at Posted at 2019-09-30

コマンド

git grep --perl-regexp --files-with-matches '検索対象のキーワード' . | uniq | xargs perl -e "s/置換対象のキーワード/置換後のキーワード/" -pi

  • カッコ () でキャプチャする
  • \1 \2 ... で置換先でキャプチャ結果を利用する
  • その他、正規表現的に使える例であれば、なんでも使えることでしょう
git grep --perl-regexp --files-with-matches '(Rec)o(rd)' . | uniq | xargs perl -e "s/(Rec)o(rd)/\1Oh\2/" -pi

結果の例

git diff

-class Job < ApplicationRecord
+class Job < ApplicationRecOhrd

解説

$ git grep -r -l 'Record' .
app/models/application_record.rb

git grep のオプション

$ git grep --help
GIT-GREP(1)                                                Git Manual                                                GIT-GREP(1)

NAME
       git-grep - Print lines matching a pattern

SYNOPSIS
       git grep [-a | --text] [-I] [--textconv] [-i | --ignore-case] [-w | --word-regexp]
                  [-v | --invert-match] [-h|-H] [--full-name]
                  [-E | --extended-regexp] [-G | --basic-regexp]
                  [-P | --perl-regexp]
                  [-F | --fixed-strings] [-n | --line-number] [--column]
                  [-l | --files-with-matches] [-L | --files-without-match]
                  [(-O | --open-files-in-pager) [<pager>]]
                  [-z | --null]
                  [ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
                  [--max-depth <depth>] [--[no-]recursive]
                  [--color[=<when>] | --no-color]
                  [--break] [--heading] [-p | --show-function]
                  [-A <post-context>] [-B <pre-context>] [-C <context>]
                  [-W | --function-context]
                  [--threads <num>]
                  [-f <file>] [-e] <pattern>
                  [--and|--or|--not|(|)|-e <pattern>...]
                  [--recurse-submodules] [--parent-basename <basename>]
                  [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
                  [--] [<pathspec>...]

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?