开发者

Modify conf file with shell script

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 call开发者_开发问答ed 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


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 ...


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


sed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜