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

grep置換

Posted at

grep 置換するときのコマンドを覚えきれなくて

grep置換
$ grep -lr "検索文字列" 検索パス | sort | uniq | xargs perl -e "s/検索文字列/置換文字列/" -pi

~/.bash_profile に関数化した。

~/.bash_profile
# 下記を追加
function grepr() {
        if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] ; then
                echo "第1引数:検索パス、第2引数:検索文字列、、第3引数:置換文字列を入力してください。"
                return 1
        fi

        grep -lr "$2" $1 | sort | uniq | xargs perl -e "s/$2/$3/" -pi
}

$ source ~/.bash_profile

# ./foo/barフォルダ以下のファイル内の文字列:"baz"を、"qux" に置換する
$ grepr ./foo/bar baz qux
1
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
1
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?