Using grep and sed to find and replace a string

后端 未结 8 1802
耶瑟儿~
耶瑟儿~ 2020-11-30 18:41

I am using the following to search a directory recursively for specific string and replace it with another:

grep -rl oldstr path | xargs sed -i \'s/oldstr/ne         


        
8条回答
  •  青春惊慌失措
    2020-11-30 19:26

    You can use find and -exec directly into sed rather than first locating oldstr with grep. It's maybe a bit less efficient, but that might not be important. This way, the sed replacement is executed over all files listed by find, but if oldstr isn't there it obviously won't operate on it.

    find /path -type f -exec sed -i 's/oldstr/newstr/g' {} \;
    

提交回复
热议问题