Using grep and sed to find and replace a string

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

    I think that without using -exec you can simply provide /dev/null as at least one argument in case nothing is found:

    grep -rl oldstr path | xargs sed -i 's/oldstr/newstr/g' /dev/null
    
    0 讨论(0)
  • 2020-11-30 19:35

    Not sure if this will be helpful but you can use this with a remote server like the example below

    ssh example.server.com "find /DIR_NAME -type f -name "FILES_LOOKING_FOR" -exec sed -i 's/LOOKINGFOR/withThisString/g' {} ;"

    replace the example.server.com with your server replace DIR_NAME with your directory/file locations replace FILES_LOOKING_FOR with files you are looking for replace LOOKINGFOR with what you are looking for replace withThisString with what your want to be replaced in the file

    0 讨论(0)
提交回复
热议问题