user290043
user290043

Reputation:

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

Upvotes: 7

Views: 18674

Answers (3)

Paul Rubel
Paul Rubel

Reputation: 27222

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

Upvotes: 11

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

sed

Upvotes: 2

Moystard
Moystard

Reputation: 1837

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

Upvotes: 4

Related Questions