Modify conf file with shell script

2019-03-29 20:42发布

I have a default conf file that we use over and over again in our projects. And, for each project, the file has to be modified. On more than one occasion, the person editing the conf file made time consuming mistakes.

So, I wanted to write a shell script that can be called to modify the conf file.

But, being new to shell scripts, I don't know how to do this. What is the appropriate *nix tool to open a text file, find a string, replace it with another and then close the text file.

Thanks! Eric

标签: shell
3条回答
神经病院院长
2楼-- · 2019-03-29 20:53

You should have a look at the sed command. It allows to edit a stream (a file for example) so you can substitute, insert, remove text.

http://www.grymoire.com/Unix/Sed.html

查看更多
虎瘦雄心在
3楼-- · 2019-03-29 20:59

As noted by other commenters, sed, is the typical tool.

Here's an example of an in-place (the -i option) edit of a file:

sed -i 's/Release Two/Testing Beta/g' /path/to/file.txt

You're replacing instances of the first string, Release Two, with Testing Beta everywhere in the files. The leading s says search/replace and the trailing g says do it as many times as it can be found (the default is to do it just once.) If you want to make a backup you can call

sed -iBACKUP_SUFFIX ...
查看更多
Emotional °昔
4楼-- · 2019-03-29 21:08
登录 后发表回答