LoginSignup
6
6

More than 5 years have passed since last update.

任意の文字列を含むファイルのみsed置換するワンライナー

Last updated at Posted at 2014-11-27

ag(silver_searcher)で検索してヒットしたファイルのみを対象にsedで置換

$ ag -l HogeClass | xargs sed -i 's/group_result: true/group_by: :result/'

ag -lはヒットしたファイル名のみを出力する.

man
AG(1)
NAME
       ag - The Silver Searcher. Like ack, but faster.
# ...
       -l --files-with-matches:
           Only print filenames containing matches, not matching lines. An empty query will print all files that would be searched.

sed -iは編集結果をファイルに反映する.

man
SED(1)
NAME
       sed - stream editor for filtering and transforming text
# ....
      -i[SUFFIX], --in-place[=SUFFIX]
             edit files in place (makes backup if extension supplied)
6
6
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
6
6