Find and replace in shell scripting

前端 未结 7 2221
暖寄归人
暖寄归人 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条回答
  •  自闭症患者
    2021-02-01 09:55

    You can use sed to do this:

    sed -i 's/toreplace/yoursetting/' configfile 
    

    sed is probably available on every unix like system out there. If you want to replace more than one occurence you can add a g to the s-command:

    sed -i 's/toreplace/yoursetting/g' configfile 
    

    Be careful since this can completely destroy your configfile if you don't specify your toreplace-value correctly. sed also supports regular expressions in searching and replacing.

提交回复
热议问题