Find and replace in shell scripting

You can use sed to perform search/replace. I usually do this from a bash shell script, and move the original file containing values to be substituted to a new name, and run sed writing the output to my original file name like this:

#!/bin/bash
mv myfile.txt myfile.txt.in

sed -e 's/PatternToBeReplaced/Replacement/g' myfile.txt.in > myfile.txt.

If you don't specify an output, the replacement will go to stdout.


sed -i 's/variable/replacement/g' *.conf

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.


Sure, you can do this using sed or awk. sed example:

sed -i 's/Andrew/James/g' /home/oleksandr/names.txt