Find and replace in shell scripting

前端 未结 7 2125
暖寄归人
暖寄归人 2021-02-01 09:20

Is it possible to search in a file using shell and then replace a value? When I install a service I would like to be able to search out a variable in a config file and then repl

7条回答
  •  -上瘾入骨i
    2021-02-01 09:57

    filepath="/var/start/system/dir1"
    searchstring="test"
    replacestring="test01"
    
    i=0; 
    
    for file in $(grep -l -R $searchstring $filepath)
    do
      cp $file $file.bak
      sed -e "s/$searchstring/$replacestring/ig" $file > tempfile.tmp
      mv tempfile.tmp $file
    
      let i++;
    
      echo "Modified: " $file
    done
    

提交回复
热议问题