Using grep and sed to find and replace a string

后端 未结 8 1801
耶瑟儿~
耶瑟儿~ 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:33

    I have taken Vlad's idea and changed it a little bit. Instead of

    grep -rl oldstr path | xargs sed -i 's/oldstr/newstr/g' /dev/null
    

    Which yields

    sed: couldn't edit /dev/null: not a regular file
    

    I'm doing in 3 different connections to the remote server

    touch deleteme
    grep -rl oldstr path | xargs sed -i 's/oldstr/newstr/g' ./deleteme
    rm deleteme
    

    Although this is less elegant and requires 2 more connections to the server (maybe there's a way to do it all in one line) it does the job efficiently as well

提交回复
热议问题