7

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

3 Answers 3

11

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 ...
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

2

sed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.